![]() |
shopping cart (0 item) |
||||||
|
|||||||
![]() ![]()
|
Create user login in PHPCreate members pages on your website within minutes with Member Login script. PHP is a good alternative when you decide to add a password protected web pages on your web site. You can also use htaccess password protection but with PHP you can create a lot more complex and configurable protection. In this example I will use SESSION variables for login verification. Lets start with building a configuration file for setting up all the username/password combinations. Create a new passwords.php file and add the following code in it. <?php $USERS["username1"] = "password1"; $USERS["username2"] = "password2"; $USERS["username3"] = "password3"; function check_logged(){ global $_SESSION, $USERS; if (!array_key_exists($_SESSION["logged"],$USERS)) { header("Location: login.php"); }; }; ?> Above code creates an $USER array with 3 username/password combinations. We also did a function which will be used later to check if an user is logged in or not. What we need now is a login page (called login.php) where users will enter their username and password and will login. <?php session_start(); include("passwords.php"); if ($_POST["ac"]=="log") { /// do after login form is submitted if ($USERS[$_POST["username"]]==$_POST["password"]) { /// check if submitted username and password exist in $USERS array $_SESSION["logged"]=$_POST["username"]; } else { echo 'Incorrect username/password. Please, try again.'; }; }; if (array_key_exists($_SESSION["logged"],$USERS)) { //// check if user is logged or not echo "You are logged in."; //// if user is logged show a message } else { //// if not logged show login form echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> '; echo 'Username: <input type="text" name="username" /><br />'; echo 'Password: <input type="password" name="password" /><br />'; echo '<input type="submit" value="Login" />'; echo '</form>'; }; ?> In order to use the user login feature for your PHP files you need to put that code at the very top of each of your PHP files that need to be protected. <?php session_start(); /// initialize session include("passwords.php"); check_logged(); /// function checks if visitor is logged. If user is not logged the user is redirected to login.php page ?> your page code goes here Create members pages on your website within minutes with Member Login script. COMMENTS
Jessica Swick When I use the logout script I get this error "Parse error: syntax error, unexpected ')', expecting T_VARIABLE or '$' in /home/southcam/public_html/admin/_assets/includes/logout.php on line 4" ----------------- Veselin: take a look at www.php.net/unset for unset() usage Line 4 = unset(); yamini i really kike the way you demonstrated the code. thanx Daniel Excellent post! Thank you all wery much! All works like a charm. The only problem I had with the logout page code. I played arround a bit and this works for me: <?php session_start(); $_SESSION["logged"]=''; echo "You are logged out"; ?> Especial thanks to Veselin on all his efforts :) Cheers! Mike Tree Hello, I am BRAND NEW at this PHP game, can Anyone explain me how it works, I want to make a page where I can have people Sign up, and Than after email verified, (or not if not possible) and LOG IN. I am a little familiar with HTML, stuff. but php or SQL is new to me :D. >>>? venkat i want php user login page Rob Going back to the post on 2008-02-08 21:45:08, is there an example that you can show us on how to query users and passwords from the MySQL database instead of using an array? Thanks! --------------- Veselin: this is a piece of code that I usually use to do it if ($ac=='logout') { $_SESSION["userID"] = '0'; } elseif ($ac=='login') { $sql = "SELECT * FROM userstable WHERE username='".$_REQUEST["username"]."' AND password='".$_REQUEST["password"]."'"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); if (mysql_num_rows($sql_result)>0) { $USER = mysql_fetch_assoc($sql_result); $_SESSION["userID"] = $USER["id"]; } else { $message = '<strong style="color:#FF0000">Incorrect login details.</strong>'; $_SESSION["userID"] = ""; unset($_SESSION["userID"]); }; }; Mary McGowan interested in login Rob THIS WORKED LIKE A CHARM!!! THANK YOU, THANK YOU, THANK YOU!! One question I have is though, is there a way to make the username and password not case sensitive? -------------- Veselin: array_key_exists() function used is case sensitive. This is wht you should define all user names and passwords in lowercase. Then change login.php page instead of if ($USERS[$_POST["username"]]==$_POST["password"]) { $_SESSION["logged"]=$_POST["username"]; it should be $tmpUser = strtolower($_POST["username"]); if ($USERS[$tmpUser]==strtolower($_POST["password"])) { $_SESSION["logged"]=strtolower($_POST["username"]); Spencer thank you, but on the logout code, do i make a link to it? and on the other post,do i put this on the passwords.php and login.php or just the users page (s)? $PAGE["username1"] = true $PAGE["username3"] = true; and then change check_logged() function to function check_logged(){ global $_SESSION, $USERS; if (!array_key_exists($_SESSION["logged"],$USERS) OR !array_key_exists($_SESSION["logged"],$PAGE)) { header("Location: login.php"); }; }; ------------ Veselin: you need to create a new page logout.php and put the code in it. Then create logout link which points to this logout.php page You need to put the $PAGE array on pages which user can access. For each page you can allow the users that can access it by creating $PAGE array element with their username. And finally change the line in function in passwords.php with the one I sent you. spence okay i got everything to work but the logout page, explain please how to make it so users can logout ---------------------- Veselin: logout code can be found in my post on 2008-02-09 08:12:39 POST A COMMENT
|