A basic login logout system with session
Category : PHP Programming Tags : php script tutorial
This is a very simple Login/Logout system script which uses session.
It only uses 1 set of username and password which is set in the php script. I hope you could get some idea on how a login/logout system basically work.
Just copy and paste the php script below and test it yourself.
PHP Code
<?
session_start();
$do=$_GET["do"];
if($_POST)
{
if($_POST["name"]=='admin' && $_POST["pass"]='pass')
{
$_SESSION["username"]='Admin';
}
}
if($do=='logout')
{
session_destroy();
unset($_SESSION["username"]);
}
?>
<html>
<head>
<title>A Sample Login Script | PHPGame.Net</title>
</head>
<body>
// ======== IF LOGIN SESSION NOT EXIST-REQUEST TO LOGIN
if(!$_SESSION["username"] && $do!='logout')
{
print " Your are currently not login.<br/>Please login with your username and password below
<form action='' method='post'>
Username <input type='text' name='name' size='20'/> (admin).<br/>
Password <input type='password' name='pass' size='20'/> (pass).<br/>
<input type='submit' value= ' Login '/>
</form>";
}
// ======== LOGOUT
if($do=='logout')
{
print "You have successfully logout.<br/>
<a href='index.php'>Click here to main page</a>";
}
// ======== LOGIN SESSION EXIST-YOU CAN ONY SEE THIS AREA ONCE YOU LOGIN CORRECTLY
if($_SESSION["username"])
{
print "Welcome to the Admin area<br/><br/>
<a href='?do=logout'>Logout now</a>";
}
?>
</body>
</html>
$do=$_GET["do"];
if($_POST)
{
if($_POST["name"]=='admin' && $_POST["pass"]='pass')
{
$_SESSION["username"]='Admin';
}
}
if($do=='logout')
{
session_destroy();
unset($_SESSION["username"]);
}
?>
<html>
<head>
<title>A Sample Login Script | PHPGame.Net</title>
</head>
<body>
// ======== IF LOGIN SESSION NOT EXIST-REQUEST TO LOGIN
if(!$_SESSION["username"] && $do!='logout')
{
print " Your are currently not login.<br/>Please login with your username and password below
<form action='' method='post'>
Username <input type='text' name='name' size='20'/> (admin).<br/>
Password <input type='password' name='pass' size='20'/> (pass).<br/>
<input type='submit' value= ' Login '/>
</form>";
}
// ======== LOGOUT
if($do=='logout')
{
print "You have successfully logout.<br/>
<a href='index.php'>Click here to main page</a>";
}
// ======== LOGIN SESSION EXIST-YOU CAN ONY SEE THIS AREA ONCE YOU LOGIN CORRECTLY
if($_SESSION["username"])
{
print "Welcome to the Admin area<br/><br/>
<a href='?do=logout'>Logout now</a>";
}
?>
</body>
</html>
| Comments | |
| john 9 Nov 2009 | this scrips doesnt work... |
| thegauraw 24 Jan 2010 | This script works fine. if u replace <? with <?php.... if u want to redirect u can use location: header() |
| David 12 Mar 2010 | I had a problem when I used only the session_destroy() and unset lines. I hadn't realized you are required to start the session - session_start() - BEFORE destroying it.
Just for those who might make the same error I did. |
| G 18 Apr 2010 | shouldn' t the "$do" be have a separate varible in the first line |
| christian 19 Jul 2010 | we are having our mini thesis w/c is ONLINE LOG IN LOG OUT SYSTEM
CAN YOU HELP USSS.. |
Post Your Comment Here