Simple PHP Login Script using Session

 

Note: Download the script Below

Are you looking for a very simple login script which does not interact with the database?.

No database used in this script. This script is useful if you want to develop a simple admin page / report page.

The workflow of the script is like True or False concept. If the session value is present then show the secret page or go to the login page!

Here is the entire login and logout script!


<?php
//simple PHP login script using Session
//start the session * this is important
session_start();

//login script
if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'login'){

//give your login credentials here
if($_REQUEST['uname'] == 'admin' && $_REQUEST['pass'] == 'admin123')
$_SESSION['login_user'] = 1;
else
$_SESSION['login_msg'] = 1;
}

//get the page name where to redirect
if(isset($_REQUEST['pagename']))
$pagename = $_REQUEST['pagename'];

//logout script
if(isset($_REQUEST['ch']) && $_REQUEST['ch'] == 'logout'){
unset($_SESSION['login_user']);
header('Location:login.php');
}
if(isset($_SESSION['login_user'])){
if(isset($_REQUEST['pagename']))
header('Location:'.$pagename.'.php');
else
header('Location:index.php');
}else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple PHP Login Script using Session</title>
</head>
<body>
<table width="850" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td>&nbsp;</td></tr>
<tr><td>
<form name="login_form" method="post" action="">
<table align="center" style="background-color:#fff;">
<tr><td colspan="2" align="center"><strong>Login Details</strong></td></tr>
<tr><td>Username</td><td><input type="text" name="uname" id="uname"></td></tr>
<tr><td>Password</td><td><input type="password" name="pass" id="pass"></td></tr>
<tr>
<td colspan="2" align="center">
<p style="color:red;">
<?php
//display the error msg if the login credentials are wrong!
if(isset($_SESSION['login_msg'])){
echo 'Wrong username and password !';
unset($_SESSION['login_msg']);
}
?>
</p>
</td></tr>
<tr><td align="center" colspan="2"><input type="submit" value="Login"></td></tr>
</table>
<input type="hidden" name="ch" value="login">
</form>
</td></tr>
</table>
<?php } ?>
</body>
</html>

How to use this script?.

Save the above code as ‘login.php’ . Then just use the below lines of code in any of your php page!


<?php session_start();
//check logged in or not!
if(!isset($_SESSION['login_user'])){
header('Location:login.php?pagename='.basename($_SERVER['PHP_SELF'], ".php"));
}
?>
<html>
<head><title>Simple PHP Login Script Using Session - Index page</title></head>
<table width="850" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<div style="float:left;"><a href="login.php?ch=logout">Logout</a></div>&nbsp;&nbsp;&nbsp;
</td>
</tr>
<tr>
<td>
<h1>Your Content will come here!</h1>
</td>
</tr>

</table>
</head>
</html>

You can download this script from the below link and can use it in any of your project immediately!.

Download   Demo

Here is another advanced tutorial – jQuery Ajax Login and Signup form – PHP Script!

About the Author

agurchand

Software Engineer, Blogger etc., Visit my Youtube channel - <a href="http://www.youtube.com/agurchand">Click Here</a>

23 thoughts on “Simple PHP Login Script using Session

Leave a Reply

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