- Colection of 65 PHP scripts for $4.29 each
Create user login in PHP
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
161 Comments to "Create user login in PHP"





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