PHPjabbers - tools for webmasters
shopping cart (0 item)
HOME
PRODUCTS
PHP TUTORIALS
PHP FORUM
ABOUT US
CONTACT US
Our products

Create user login in PHP

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
ale


how to Check, if user is already login a user in a login page, then jump to secured page
kiesy dorothy

i would like to know how u create a login and log out form using php with characters like first name ,last name etc
mohit

hi,
is there a way i can echo out the logged
Bhai Dupare

Parse error: syntax error, unexpected T_STRING, expecting '(' in /home/a8096787/public_html/Downloads.html on line 191
uma

how to set user picture in home page using php coding
Gildas

Am sure this will help me on my site thanks a lot!
TJ Hemingway

First off - thanks so much. I really am trying to make sense of this and I know I am getting close. If someone has a moment to look at my login and see if there is something obvious I am overlooking, I would be immensely grateful. The login/logout works but with warnings. http://www.tjhemingway.com/login.php . I have a passwords.php, login.php and logout.php. All are getting a header error of some sort. The redirect is not going to the respective pages. One of the pages I want it to redirect to is http://www.tjhemingway.com/proofs/AshleyH/index.php I am getting an error with that as well. I have tried all sorts of variations from previous postings but they don't seem to be working for me. Any assistance is welcomed!!
SBUDAkasonecia

how to design a php script that contains parallel arrays named $usernames and $passwords containing ten usernames and passwords respectively.the script must accept and verify the login details from the form against ten usernames and passwords in the arrays.
shankar

<?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>';
};
?>
ar

how safe is this?
sravs

how to Check, if user is already login a user in a login page, then jump to secured page
kiesy dorothy

i would like to know how u create a login and log out form using php with characters like first name ,last name etc
 
POST A COMMENT
Your name:
Your email: (email address will not be posted on the website)
Comment:
Verification code:
type characters on the image in the text box