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
Matt

I get this someone please help me.


Warning: main(passwords.php): failed to open stream: No such file or directory in c:\\program files\\easyphp\\www\\login.php on line 3

Warning: main(): Failed opening \'passwords.php\' for inclusion (include_path=\'.;C:/Program Files/EasyPHP\\php\\pear\\\') in c:\\program files\\easyphp\\www\\login.php on line 3

Notice: Undefined variable: USERS in c:\\program files\\easyphp\\www\\login.php on line 5
Incorrect username/password. Please, try again.
Notice: Undefined variable: USERS in c:\\program files\\easyphp\\www\\login.php on line 11

Warning: array_key_exists(): The second argument should be either an array or an object in c:\\program files\\easyphp\\www\\login.php on line 11
Spencer K

Hi,

How do you make a logout thing?

Any suggestions?
Spencer
Zoomit

If I wanted to write this in my program... Microsoft front page. how would I writeit. It doesn't support php.
sivaprasad

How to creage login page in php with form validation and using sessions. If user name and password is correct then open the new page how is it.
Joe

If you look at the 3 gray boxes above with the code. the first gray box should be pasted in a new file called passwords.php the second gray box is the form where the user will type the username and password and all the code from that box should be saved in a new file called login.php. so far you have two files and they should both be in the same directory on the webserver. then all you have to do is open up every page that you want protected by the users password you need to paste the 3rd gray box above all other text in that file.
You should have a working login now. open up the passwords.php and under all of the user arrays you paste what Dan told you above on 2007-7-27 and then his next step says to add another line so keep reading his instructions, he knows what he is talking about.
Spencer

I would like u to make me a php login thing. I cant get it to work. I need you to make it so when users log in they redirect to their own page. And also when, say ex. the go to home.php and they r not logged in, it will ask them to, then when logged in it will go to home.php

Spencer
Joe Again

i would like to know how to redirect a user to the home page if they have not logged in yet. right now if they click on a restricted area they go to a page with only the login.php form on it. some users would get lost if they had this.
Joe

Great Tutorial. I had this up and running in about 15 minutes and im a complete noob. I am using this on Ubuntu 7.04 with apache 2.2.
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
 
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