How To Exercise User Login Session Timeout Logout Inwards Php
We volition produce got it inwards to a greater extent than simplified means for your ameliorate understanding,just follow the steps inwards a known fourth dimension the functionality volition last created.
Steps To Create User Login Session Timeout Logout In PHP
1.Create a script amongst php too refer it equally functions.php amongst the below code
<?php role isLoginSessionExpired() { $login_session_duration = 10; $current_time = time(); if(isset($_SESSION['loggedin_time']) too isset($_SESSION["user_id"])){ if(((time() - $_SESSION['loggedin_time']) > $login_session_duration)){ return true; } } return false; } ?>
This tending inwards setting the session during,once the duration is exceeded the user volition last logged out automatically from the website.
2.Create mode to give our page to a greater extent than beautiful await amongst the below code too refer it styles.css
body{width:610px;} .tableheader { background-color: #95BEE6; color:white; font-weight:bold; } .tablerow { background-color: #A7D6F1; color:white; } .message { color: #333; border: #FF0000 1px solid; background: #FFE7E7; padding: 5px 20px; } .tblLogin{ margin-top : 2px; }
3.Create user logging session too refer it user_login_session.php amongst the given code below
<?php session_start(); $message=""; if(count($_POST)>0) { if( $_POST["user_name"] == "admin" too $_POST["password"] == "admin") { $_SESSION["user_id"] = $row[user_id]; $_SESSION["user_name"] = $row[user_name]; $_SESSION['loggedin_time'] = time(); } else { $message = "Invalid Username or Password!"; } } if(isset($_SESSION["user_id"])) { header("Location:user_dashboard.php"); } ?> <html> <head> <title>User Login</title> <link rel="stylesheet" type="text/css" href="styles.css" /> </head> <body> <form name="frmUser" method="post" action=""> <div class="message"><?php if($message!="") { echo $message; } ?></div> <table border="0" cellpadding="10" cellspacing="1" width="500" align="center"> <tr class="tableheader"> <td align="center" colspan="2">Enter Login Details</td> </tr> <tr class="tablerow"> <td align="right">Username</td> <td><input type="text" name="user_name"></td> </tr> <tr class="tablerow"> <td align="right">Password</td> <td><input type="password" name="password"></td> </tr> <tr class="tableheader"> <td align="center" colspan="2"><input type="submit" name="submit" value="Submit"></td> </tr> </table> </form> </body> </html>
4.Also produce user logout session amongst the code below too refer it logout.php
<?php session_start(); unset($_SESSION["user_id"]); unset($_SESSION["user_name"]); $url = "index.php"; if(isset($_GET["session_expired"])) { $url .= "?session_expired=" . $_GET["session_expired"]; } header("Location:$url"); ?>
This script tending us to destroy the session when it has expired too clit out the user from beingness logged into the website.
5.Next follow past times creating the user dashboard too refer it user_dashboard.php with the below code
<?php session_start(); include("functions.php"); if(isset($_SESSION["user_id"])) { if(isLoginSessionExpired()) { header("Location:logout.php?session_expired=1"); } } ?> <html> <head> <title>User Login</title> <link rel="stylesheet" type="text/css" href="styles.css" /> </head> <body> <table border="0" cellpadding="10" cellspacing="1" width="100%"> <tr class="tableheader"> <td align="center">User Dashboard</td> </tr> <tr class="tablerow"> <td> <?php if(isset($_SESSION["user_name"])) { ?> Welcome <?php echo $_SESSION["user_name"]; ?>. Click hither to <a href="logout.php" tite="Logout">Logout. <?php } ?> </td> </tr> </body> </html>
6.Finally produce an index.php code amongst the given code below
<?php session_start(); include("functions.php"); $message=""; if(count($_POST)>0) { if( $_POST["user_name"] == "admin" too $_POST["password"] == "admin") { $_SESSION["user_id"] = 1001; $_SESSION["user_name"] = $_POST["user_name"]; $_SESSION['loggedin_time'] = time(); } else { $message = "Invalid Username or Password!"; } } if(isset($_SESSION["user_id"])) { if(!isLoginSessionExpired()) { header("Location:user_dashboard.php"); } else { header("Location:logout.php?session_expired=1"); } } if(isset($_GET["session_expired"])) { $message = "Login Session is Expired. Please Login Again"; } ?> <html> <head> <title>User Login Session Timeout Logout inwards PHP</title> <link rel="stylesheet" type="text/css" href="styles.css" /> </head> <body> <form name="frmUser" method="post" action=""> <?php if($message!="") { ?> <div class="message"><?php echo $message; ?></div> <?php } ?> <table border="0" cellpadding="10" cellspacing="1" width="100%" class="tblLogin"> <tr class="tableheader"> <td align="center" colspan="2">Enter Login Details</td> </tr> <tr class="tablerow"> <td align="right">Username</td> <td><input type="text" name="user_name"></td> </tr> <tr class="tablerow"> <td align="right">Password</td> <td><input type="password" name="password"></td> </tr> <tr class="tableheader"> <td align="center" colspan="2"><input type="submit" name="submit" value="Submit"></td> </tr> </table> </form> </body> </html>
Having follow the steps accordingly,then you lot produce got built a uncomplicated user login session timeout inwards PHP