« Make php counterProtect email address from spam bots »

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

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.

154 Replies to "Create user login in PHP"

jignesh gajeragmail.com October 20, 2014 at 7:49 pm | Reply

0

grate job keep it up gyzzzzz.
jignesh gajeragmail.com October 20, 2014 at 7:48 pm | Reply

0

grate job keep it up gyzzzzz.
jignesh gajeragmail.com October 20, 2014 at 7:48 pm | Reply

0

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

+2

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

0

:) 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

+2

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

0

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

+9

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

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

<?php
$user = "admin";
$pass = "password";
?>


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

<?php
include("config.php");

// 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
session_start();
// 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">
</form>';
}
?>


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


<?php
include("config.php");
// Start session
session_start();

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

<?php
session_start();
session_unset();
session_destroy();
unset($_SESSION['logat']);
header("Location: ./");
?>


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

0

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

+4

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"]) {
$_SESSION["logged"]=$_POST["username"];


LINE 17 IN LOGIN.PHP:
if (array_key_exists($_SESSION["logged"],$USERS)) {
header("Location: ing.php");
David March 13, 2014 at 9:34 am | Reply

+3

am new to PHP and this is a lifesaver! Thank you so much. It works great!
Kyle Pastor February 26, 2014 at 11:11 pm | Reply

+1

Simple and clean :D Thanks!


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 ~