« Make php counterProtect email address from spam bots »

Create user login in PHP Posted in PHP Tutorials | 156 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.

$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.

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
} 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.

session_start(); /// initialize session
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

Do you know PHP / HTML / CSS / JS well?

Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your experience with millions of other webmasters visiting our website. Contact us for more information how to become a contributor.

156 Replies to "Create user login in PHP"

Andrei November 5, 2014 at 1:06 pm | Reply


who need realy help... email me on seo@lunetistii.ro
Robert November 4, 2014 at 8:49 pm | Reply


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 November 3, 2014 at 5:05 am | Reply


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 November 3, 2014 at 5:02 am | Reply


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 October 20, 2014 at 7:48 pm | Reply


grate job keep it up gyzzzzz.
Aimelo Malisa August 7, 2014 at 8:29 am | Reply


I just want to ask on how to make a basic login page then how to make sessions
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 July 3, 2014 at 2:04 pm | Reply


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..
tanvir May 25, 2014 at 7:48 am | Reply


it works for the given username and password but when i change the username and password then it does not work.how could i change the username and password?
Andrei May 22, 2014 at 2:47 am | Reply


I created other tutorial for all who don't need mysql

Tutorial is sample...
Step 1:
Create config.php file and write intro :

$user = "admin";
$pass = "password";

Step 2.
Create index.php file and write intro :


// Check form if is submited
if(isSet($_POST['trimite'])) {
// Check if user is equal with username and password from config.php
if($_POST['user'] != $user || $_POST['pass'] != $pass) {
echo "Sorry, your data is invalid";
} else {
// Open the session for store user logged
// Setting the session
$_SESSION['logat'] = "da";
// Redirecting user to admin page if is logged
Header('Location: admin.php');
} else {
// Form
echo '<form action="" method="post">
Username: <input type="text" name="user"><Br />
Password: <input type="password" name="pass"><br />
<input type="submit" name="trimite">

Step 3.
Create admin.php file and write intro ;

// Start session

// Check if user is logged and existing session
if(isset($_SESSION['logat'])) {
// Content for user logged
echo "Welcome ".$user." :) - <a href='logout.php'>Logout</a>";
} else {
// Redirecting to login page
Header("Location: ./");

Verry good. That is all.
And if you need logout page... have this

header("Location: ./");

This tutorial is created by me! :)
Majid August 8, 2014 at 2:08 pm


how to create user profile page with login and password.
Hum May 14, 2014 at 8:02 am | Reply


Can anyone help me with this please?

The code is working, it detect the correct user and password, but I have the next problem:

In my page i see a user and password box to login, so between this i have this errors:
Notice: Undefined index: ac in C:\xampp\htdocs\xampp\paginas\dato_from_server\login.php on line 10

Notice: Undefined index: logged in C:\xampp\htdocs\xampp\paginas\dato_from_server\login.php on line 17

LINE 10 in login.php:
if ($_POST["ac"]=="log") {
if ($USERS[$_POST["username"]]==$_POST["password"]) {

if (array_key_exists($_SESSION["logged"],$USERS)) {
header("Location: ing.php");

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

Log in your free account or if you still haven't joined 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 ~