PHPjabbers - tools for webmasters Stay in touch
HOME
ORDER
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
Hasan Mahmud Riyad

Hello there, thank you for your valueable effort. i need some suggestion about PHP and SQL. i need to create a user authentication after successfull authenticate user have to open specific page, and no one (no user) could not open or read that page. what should i do? Have any suggestion ...:)
-------------------------
Veselin: First, you can create a new ARRAY with pages for each username
$URLS["page1.php"] = 'username1'
Then on top of each page you need to create a function which checks if the username belong to that page.

$currentPage = 'page1.php';
if ($URLS[$currentPage]=$_SESSION["logged"]) {
&nbsp;&nbsp;&nbsp; echo "Allowed";
} else {
&nbsp;&nbsp; echo "Not Allowed";
};

For each page you need to change the $currentPage variable to page name.

ranu

Can any body solved this problem

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\gandhinews.com\login\news.php:1) in
-----------------
Veselin: put session_start() at the very bottom of your php script
Dibya Prakash Sahoo

I want to create a login page for my website. So that registered users can only see my webpages for that I require the concept of session. So please send me the code in php how to create a session.
--------------------
Veselin: take a look at http://www.php.net/session for information about how to create session variables and use them
Aviv Hadar

Hi there,

The script is amazing ! I was just wondering what has to be done so that every user name is assigned to be directed to a different URL. I have done everything mentioned in the tutorial and also in the comments section of the tutorial page. I haven\'t incorporated this into my completed website yet. I was wondering if you had to charge me to look at my code or what you could do to help me. Basically, I want my clients to be able to login and either be redirected to a page I have built for them, or be redirected to the website that I am working on for them. Please get back to me as fast as you can.
-----------
Veselin: please, send me login details and I will take a look and then give you a quote if it is too time consuming
Mike

no no i want to know where in the script do i put the page where my users will be directed after sucessfully log in
Mike

Hi Veselin Im Currently Using your Sites Login System But Im Having A Little bit of trouble with it ..
I have everything set up right but I dont know where to put the page i want my users to be directed to after login could you help me? thanks alot
Mike
--------------------
Veselin: "where to put the page i want my users to be directed to" - this can be any web page on your website
zylon

For all of you that say that you have tried the php code that Veselin created and are having issues - can I suggest (without offending people) that you make sure that you are using a pure text editor - that you copy and paste the code EXACTLY asis - and I hate to say it - read the instructions. This code works flawlessly.
michele

i have a few questions can i make it where i give the usernames and passwords and be able to redirect them to certain pages for the specific user please email me back at bthibodeaux@tlrlegalresearch.com and mpeele@tlrlegalresearch
Thank you for the help
Janice Valletta

OOOO never mind! if itsin the password file this is a null issue. My mistake. Works beautifully.
Janice Valletta

I only have one error, and thats in the header for the redirect. I amworried that somehow its not getting the second " mark. I get redirected to a null webpage.
header("Location:" .$REDIRECT[$temp]);


Doesnt Location have to have a second quotation? as such:
header("Location: http://www.$site_registered_to");

The above code loses the last and I cant seem to add it back in.
 
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