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
mohit

hi,
is there a way i can echo out the logged in user's name after logging in??
shee

can i use this password?
Mybibo

please tell me:
How login user see his name in html page.
exm.. login.php > myaccount.html
what is the code???
help me!
Dave

I am looking to make a terms and conditions page, I already have the form and the checkbox that they must check but do not know how to write it in Php so they go to a set page depending on condition of whether box checked or not? Grateful for some help?
furqan ali

mera adhar number hame nahi mila hai jabki mera enrolment 4/1/2011 ko huwa tha
Brandon

Is it pausible to connect this to a MySQL Database? If so, may you please help me?
harpreet dhiman

oh... very gud site to learn php
Alex

I would like to know how to create a dynamically generated php page specific to a user - like what you'd have in a social network or online banking site.
TopWebGuy

This is a very simple, efficient lean solution.
I have implemented a number of user authentication mechanism in PHP and many in other languages and tonight I was working on a friend's company small app and needed to put some authentication quickly.
And I think your solution provides one of the solutions with least files and lines of code. I only added a redirect to index.php after the successful login. I am also planning in the future to put user ids and passwords in the database so they are maintainable by an admin.
Though we should have in mind that hard coding the passwords in the code is an automatic protection against SQL injections at login time. SQL injections can be protected other ways too, of course.
wajid

thanks dear to provider info
 
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