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>


Comments
john
9 Nov 2009
this scrips doesnt work...
thegauraw
24 Jan 2010
This script works fine. if u replace &lt;? with &lt;?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 &quot;$do&quot; 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..
Emeka David
5 Nov 2010
Please here i have a script that throws requires a login page for a guest that is not a registered member of the site but after registering and wants to click on that page that requires the login, he page will require the page again when the person must have registered and logged in. please i need help here. i'll be glad please
kristian shane
18 Nov 2010
walang kasing ganda savior ka talaga
kristian shane
18 Nov 2010
im kristian shane forro huesca!! a comsci student in iloilo
salamat sa codes ha!!
mine
23 Jan 2011
hfhh
bhabhie Hikaru
3 Mar 2011
can u help us to know the objective of log in log out system..plzzzz..thanks...
Powerslave
18 Jun 2011
Oh man you are kidding, don't you?! You should never hard code any passwords or other login information, and using unencrypted login data is strongly discouraged. You seems to teach WRONG THINGS to people who are even less familiar with web programming than you.
sakthi
3 Aug 2011
data lost in php.
if i sent a session variable to next next pages, it gets lost
i dont know y?
will u help me with sample coding(full coding plz)


Post Your Comment Here

Your Name
Email Address
Homepage
Custom Search


Make Web Game Tutorial
Part 1: How to make a web game
Part 2: Learn HTML to make your webite
Part 3: How to use CSS to format your website

Ads