go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

Create user login in PHP

Tuesday, 6th December, 2011  /  PHP Tutorials  /  161 Comments
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
Share on:

161 Comments to "Create user login in PHP"

jesma

jesma / December 15, 2017 at 06:44 am

how to make login page in php with mysql databse connection ? plz help!!

i created an acount but cant login showing that account not confirmed

i created an acount but cant login showing that account not confirmed / May 24, 2017 at 12:39 pm

help me pliz

nand arya

nand arya / September 13, 2015 at 12:57 pm

Nice knew

Mohammed Abulsoud

Mohammed Abulsoud / September 12, 2015 at 13:17 pm

Can you tell me where is the best secure place or strategy to put the database connection information?

Ronald

Ronald / May 2, 2015 at 04:51 am

Is there a comprehensive tutorial explaining which folders should contain user level access files? I just bought Member login and I don't see anything....

zeyede

zeyede / March 26, 2015 at 19:46 pm

let show the above questions

Robert

Robert / November 4, 2014 at 20:49 pm

Thank you for this quick and easy tute. This is a very basic user/login app that should be used for pre-development. Final production for public use should use a more robust security that does not save your user/password information to your FTP server. However, this is a great tool for development to prevent people from stumbling into your site accidentally and having access to your data.

There are a couple of questions regarding only showing user specific data after being logged in. This requires using a user/login/role security model. This will need to set in your data base. The role will determine what data a user has access to. Again, this tutorial is a good tutorial but shouldn't be used to try such a technical security model.

Jae

Jae / November 3, 2014 at 05:05 am

hi, i have some problem at here! how to make when user log into the system only their data display...for example.. in a table when the user log in to the system it will only show their own data not the others.. can u pls give me some of the example?


thank you for your help! :D

Jae

Jae / November 3, 2014 at 05:02 am

hi, i have some problem at here! how to make when user log into the system only their data display...for example.. in a table when the user log in to the system it will only show their own data not the others.. can u pls give me some of the example?


thank you for your help! :D

elysee

elysee / October 12, 2018 at 11:00 am

use the 'if' stament to redirect each users to it own page.

jignesh gajeragmail.com

jignesh gajeragmail.com / October 20, 2014 at 19:48 pm

grate job keep it up gyzzzzz.

Add your comment

Captcha

    Please, be polite and helpful and do not spam or offend others! We promise you will be treated the same way!

    Log in to your account to post your comments. If you still haven't joined our community yet, you can create your FREE account now!

    Posting tip:
    If you use code in your comments, please put it in these tags [php], [sql], [css], [js] PHP code example: [php] echo date("Y-m-d"); [/php]

    Thank you,
    PHPJabbers Team

    Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.


    Free scripts

    PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!


    Commercial PHP scripts