![]() |
|
||||||
|
|||||||
|
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
naditha plz help me dis is code for login.. dis shows one error..plz correct tat error <?php //always start the session before anything else!!!!!! session_start(); if(isset($_POST['username']) && $_POST['password']){ $username = $_POST['username']; //name of the text field for usernames echo $username; $password = $_POST['password']; //likewise here just for the password //connect to the db echo $password; $conn = mysql_connect('localhost', 'root', 'jani'); mysql_select_db('exam', $conn); //run the query to search for the username and password the match $query = "SELECT * FROM admin WHERE username = '$username' AND password = '$password' "; $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); //this is where the actual verification happens if(mysql_num_rows($result) == 1){ //the username and password match //so e set the session to true $_SESSION = true; //and then move them to the index page or the page to which they need to go echo "uiuio"; header('Location: new.php'); }else{ $err = 'Incorrect username / password.' ; } //then just above your login form or where ever you want the error to be displayed you just put in echo $err; ?> Spider Hey people I need a login script with php with a mysql data base to store usernames and passwords I choose, no registering. Any one know the script or where to get of from? Would be greatfully appreciated. Matt Smith I have a page that I'm unable to add the "check for logged in" code snippet. If I remove the lines of code (from box three) the page works fine, but is unprotected. Any ideas.. the code is the index.php file from snif..which is a directory listing script available here.. http://www.bitfolge.de/snif-en.html my other "protected test page" works fine..so it seems to be that the php code isn't meshing well with the snif php code. HELP? jarrett ok, i fixed that last problem. but how do i create the make an account so that users can log in? Muhammed Hanif Hello!dear your login script is working good..... but where is the LOGOUT button????? Coz i dont know php so good woll cread a Logout script for me or else who hepl me.. Thankx in advance regard Muhammed Kilo I get an T_String error on line 6 in login.php. Can some see what is wrong... The php code is.: username and password exist in $USERS array ------------ Veselin: please make sure that you only have PHP code on the lines and all the comments that we use are on a line starting with /// Fernando Colin Hi...I set up a website on one.com for fellow M.G.car enthusiasts a year ago and although I have lots of info. I,annoyingly, cannot let members see it as I can't set up user name/login page. I am afraid that I am a complete ignoramous when it comes to programming so please offer your advice very much in layman's terms. Thank you very much for your help Colin Ashish Hi Could you please help me in designing a searchable database as follows:- In my HTML page, I would like for example to have a search database, whereby if someone for example is looking to buy Houses, lands or bungalows with regard to location/city and price range. Thank you for your help. Rgds Aks ---------- Veselin: please contact us via our support system and we can discuss the project Johannes Schmidt Yes i mean when you do a back and forward in the browser. But since i like the content only to be for me, can i do something so that the content can not been seen when i lock out. EX on g-mail ehn i log out i can not see the content, it just go back to login page, when i do a back or forward in the browser, the same for my webhotel, which is 50webs. Thanks for entertaining this. ------------- Veselin: can you send me URL to your page and user login details so I can see how it works POST A COMMENT
|