|
||||||||||||||
|
||||||||||||||
Create 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 |
| abdul posted on May 13, 2010, 5:02 pm |
| well don job, please i need more example on session and cookies |
| euvegio posted on March 20, 2010, 9:44 am |
| i need more example about php |
| Thamos posted on February 27, 2010, 2:46 pm |
| These are nice tutorial for PHP beginner using this type technique most PHP developer could design the secure login form. |
| khati posted on January 31, 2010, 10:06 am |
| perfect! thanx lot! |
| sathish.r posted on December 3, 2009, 8:55 pm |
| i am satisfied. |
| DD posted on October 7, 2009, 11:52 am |
| Hi everyone.. actually I have a problem to create specific user for my final year project. I' am doing submit drawing system for architect. my problem is, I can't do: After user A upload her/his drawing and send to user B, User B only can download the drawing from user A only..Another user can't download another user's drawing to avoid the plagiarism.. how to do that using php code? help me.. thanks.. |
| Motz posted on September 15, 2009, 3:39 am |
| Why you aren't using md5 for encrypting your passwords? |
| pravin posted on August 6, 2009, 3:46 pm |
| Hi could you pls help i want to cret the login page in that one user log in and in side the login page i give him the inbox page in that all the excel file login person want to upload from the browser and get right to the user to delete the files all uploaded files are dispalyed on same page and in another html page |
| naditha posted on July 23, 2009, 1:23 pm |
| 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 posted on March 14, 2009, 2:24 pm |
| 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. |