PHP-Sessions Login Logout example
PHP-Sessions Login Logout System
This tutorial explains you How to use session in Login logout module using PHP. To implement this module, we are going to use the following files.
First create a project folder in server (htdocs/www) and create the files as shown in the below. Next, open the session_login.html in browser and type user : admin and pwd: admin and click login button. Then it is redirected to login.php page, login.php compares the values given in form with the values given in the if condition, if they are similar it stores user value in session and redirected to welcome page. Welcome page(welcome.php) checks user is set or not in session. If yes, then it greets with the message "hello admin you are logged in" and also displays a link to logout. If you click logout page unsets user value and destroys the session and redirects to the welcome page. Welcome page again checks if the user is available in session, now the user is not available in session, so it displays a message as "hello Guest" and also a link to the login page.
session_login.html
<html>
<body>
<form method="POST" action="session_login.php">
Username:<input type="text" name="user"/><br />
Password:<input type="password" name="pwd"/><br />
<input type="submit" name="submit" value="Login"/><br />
</form>
</body>
</html>
TopMenu
session_login.php
<?php
************** starting session *****************
session_start();
$user =$_POST["user"];
$pwd=$_POST["pwd"];
if($user == "admin" and $pwd == "admin")
{
$_SESSION["user"] = $user; // storing username in session
header("Location:welcome.php");
}
else
{
echo '<h2>Invalid Login Details</h2>';
}
?>
MenuTop
welcome.php
<?php
session_start();
if(isset($_SESSION['user'])) // checking username is in session { echo "<h2>Hello ", $_SESSION['user']; echo " You are logged in"; echo "<br> <br> <a href = 'logout.php' > Logout </a> "; }
else{
echo "<h1>Hello Guest ";
echo "<br> <br> <a href = 'session_login.html' > Login </a> </h1> ";
}
?>
If users logs in, welcome page displayed.
Move Top
logout.php
<?php
session_start();
unset($_SESSION['user']);
session_destroy(); // session deleted
echo "<h2>successfully logged out";
echo "<br> <br> <a href = 'welcome.php' > Home </a></h2> ";
?>
Comments
Post a Comment