go top

Create user login in PHP

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
  • 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

158 Comments to "Create user login in PHP"

  • 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

  • jignesh gajeragmail.com

    jignesh gajeragmail.com

    October 20, 2014 at 19:48 pm

    grate job keep it up gyzzzzz.

  • Aimelo Malisa

    Aimelo Malisa

    August 7, 2014 at 08:29 am

    I just want to ask on how to make a basic login page then how to make sessions

    • Puday Eter

      Puday Eter

      August 25, 2014 at 11:22 am

      :) you can't find what you want, dude better ask sir laylo your very good teacher
      just do that stuff!

  • lawal rasak

    lawal rasak

    July 3, 2014 at 14:04 pm

    Please i av created the admin, login, logout and the index page of PHP..i don't know how to merge every PHP Script with the HTML part of the login page..

Add your comment

Captcha