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
Johannes Schmidt

Hey this is Johannes again.

As i have been working around with this code, i have a couple of quistion.

OK

I follow the one you have, and when logged out, and go back and forth in the browser, i do not c any content from what was on the page.

But if a put a menu to get some information into the page, things start to be different.

ex.

the menu would be like this
<a href="?action=test">new test</a>

i then make a

if ($_GET['action'] == test) { require ("testter.php");}

this then comes on the page.
But what happens when i do a log out.......
When going back and forward i am able to c the content of some of the things on my page, and offcause the idea by this, is that only the one that has the id and pass can c it.

How do i do to work over different pages and when i do a log out, i can not go back and forth in my browser and c the content.

I am working on making a page that will upload picture and text for the user.

Hope you can help me, or give me an idea as to were i can find toturial and so on on that subject.
Have tried searching around, but did not fall over something yetm therefor me writing this.
-------------------
Veselin: my first question is about "i can not go back and forth in my browser". Do you use browser Back and Forward buttons to do this or you mean clicking on website menu links? Because if you use browser buttons you see the content because it is cached by the browser.
Johannes Schmidt

Just want to give u a very big thanks for the help, that did it. This code was realy a big help to me in understanding the meaning of using session as a part of a login out page.
Johannes Schmidt

Hey

I have been using ur suggestion to code here for login and out, and that works very perfect for me.
As i am logged in i want to change the content on my page by a action
ex.

<a href='action=test'>test</a>
then i call that by
a switch
switch ($_GET[action]) {
case "test" : require ("pagesoandso.php");break;}

This goes into a <div> that i have on the page.

It works but i do get a

Fatal error: Cannot redeclare check_logged() (previously declared in C:xampphtdocsschmidtfampassword.php:7) in C:xampphtdocsschmidtfampassword.php on line 11,

How do i get it to work under the session that i have started, so that when i am log out, i will not be able to see the content.

I have the
<?
session_start();
include("password.php");
check_logged();
?>
at the top of the fill i require.
Without it i do c the content but it is still there when i logout.

How do i do that one????
------------------
Veselin: instead of include("passwords.php"); use include_once("passwords.php");
Jessica Swick

When I use the logout script I get this error "Parse error:
syntax error, unexpected ')', expecting T_VARIABLE or '$' in /home/southcam/public_html/admin/_assets/includes/logout.php on line 4"
-----------------
Veselin: take a look at www.php.net/unset for unset() usage

Line 4 = unset();
yamini

i really kike the way you demonstrated the code.
thanx
Daniel

Excellent post!
Thank you all wery much!

All works like a charm. The only problem I had with the logout page code.
I played arround a bit and this works for me:

<?php
session_start();
$_SESSION["logged"]='';
echo "You are logged out";
?>

Especial thanks to Veselin on all his efforts :)
Cheers!
Mike Tree

Hello, I am BRAND NEW at this PHP game, can Anyone explain me how it works, I want to make a page where I can have people Sign up, and Than after email verified, (or not if not possible) and LOG IN.

I am a little familiar with HTML, stuff. but php or SQL is new to me :D. >>>?
venkat

i want php user login page
Rob

Going back to the post on 2008-02-08 21:45:08, is there an example that you can show us on how to query users and passwords from the MySQL database instead of using an array? Thanks!
---------------
Veselin: this is a piece of code that I usually use to do it

if ($ac=='logout') {
$_SESSION["userID"] = '0';
} elseif ($ac=='login') {
$sql = "SELECT * FROM userstable WHERE username='".$_REQUEST["username"]."' AND password='".$_REQUEST["password"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
$USER = mysql_fetch_assoc($sql_result);
$_SESSION["userID"] = $USER["id"];
} else {
$message = '<strong style="color:#FF0000">Incorrect login details.</strong>';
$_SESSION["userID"] = "";
unset($_SESSION["userID"]);
};
};
Mary McGowan

interested in login
 
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