Skip to content

PHP Login Page with Sessions

  • by
php login

Are you looking for PHP login script, in this post I want to discuss how to create a simple PHP login with welcome page using MySQL database. This will explain you creating admin_login tables, posting form values and storing and destroying the session values. If you are a PHP beginner download script and take a quick look at this live demo with Username : admin Password : admin. This post has been updated with mysqli.

php login

Database
MySQL admin table columns id, loginid and password.

CREATE TABLE IF NOT EXISTS `admin_login` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `loginid` varchar(30) DEFAULT NULL,
  `password` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `loginid` (`loginid`)
)

dbconfig.php
Database configuration file.

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

index.php (login form page)
Contains PHP, HTML and CSS code.

<?php
include("dbconfig.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST")
{
// loginid and password sent from form 

$loginid=mysqli_real_escape_string($db,$_POST['loginid']); 
$password=mysqli_real_escape_string($db,$_POST['password']); 


$sql="SELECT id FROM admin_login WHERE loginid='$loginid' and password='$password'";
$result=mysqli_query($db,$sql);
$count=mysqli_num_rows($result);

// If result matched $loginid and $password, table row must be 1 row
if($count==1)
{
  $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
  $active=$row['id'];

  session_register("loginid");
  $_SESSION['login_name']=$loginid;

header("location: welcome.php");
}
else 
{
$error="Your Login Name or Password is invalid";
}
}
?>
<?php 
if(isset($error)) 
{ 
   echo '<div style="font-size:11px; color:#FF0000; margin-top:10px">'.$error.'</div>'; 
} 
?>
<!--CSS CODE-->
<style type="text/css">
body {
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:14px;
}
label {
	font-weight:bold;
	width:100px;
	font-size:14px;
}
.box {
	border:#666666 solid 1px;
}
.btn {
	font-weight:bold;
	color:#FFFFFF;
	background:#0033FF;
	border-radius:3px;
	border:none;
}
</style>
<!--HTML CODE-->
<form action="" method="post">
  <label>Login ID  &nbsp;&nbsp;: &nbsp;</label>
  <input type="text" name="loginid" class="box"/>
  <br />
  <br />
  <label>Password  &nbsp;:&nbsp;</label>
  <input type="password" name="password" class="box" />
  <br/>
  <br />
  <input type="submit" value=" Submit " class="btn"/>
  <br />
</form>

lock.php
Session verification. If no session value page redirect to index.php (login page)

<?php
include('dbconfig.php');
session_start();
$user_check=$_SESSION['login_name'];

$ses_sql=mysqli_query($db,"select loginid  from admin_login where loginid='$user_check' ");

$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$login_session=$row['loginid'];

if(!isset($login_session))
{
header("Location: index.php");
}
?>

welcome.php

<?php
include('lock.php');
?>
<body>
<h1>Welcome <?php echo $login_session; ?></h1>
</body>

logout.php
SignOut Destroy the session value.

<?php
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>
PHP Login Page with Sessions

Leave a Reply

Your email address will not be published. Required fields are marked *