How to Create Login Session Timeout in PHP
Hi, in this article we are going to tell you How to Create Login Session Timeout in PHP and I want to explain how to set a user login session time out program after leaving screen for few second. if you are a beginners to develop a online program then this should your first choice to timeout the login session after leaving the screen for few second to do this we have to follow some basics code line in PHP.
How to Create Login Session Timeout in PHP
Creating Login Form Using HTML
in this step we are creating a login form.
<form method="post" action=""> <?php if($message!="") { ?> <div id="message" class="blink_text"><?php echo $message; ?></div> <?php } ?> <table border="0" cellpadding="10" cellspacing="1" class="tblLogin"> <tr class="tableheader"> <td align="center" colspan="2">Login</td> </tr> <tr class="tablerow"> <td align="right">Username</td> <td><input type="text" autofocus="autofocus" class="textbox_detail" name="user_name"></td> </tr> <tr class="tablerow"> <td align="right">Password</td> <td><input type="password" autofocus="autofocus" class="textbox_detail" name="password"></td> </tr> <tr class="tableheader"> <td align="center" colspan="2"><input type="submit" class="btn_submit" name="submit" value="Login"></td> </tr> </table> </form>
Styling The Login Form and Body Area When Loged in
in this step we are going to create style for login form and other element that’s used in this tutorial Like Loged in area and loged out.
body { width:370px; margin:auto; } a { text-decoration:none; color:black; } a:hover { color:blue; } .tableheader { background-color: skyblue; color:blue; font-weight:bold; font-size:large; } .tablerow { background-color: whitesmoke; color:blue; font-weight:bold; font-size:18px; } #message { color: white; border: red 1px solid; background: black; padding:20px; font-size:18px; font-weight:bold; margin-top: 31px; border-radius:4px; width: 305px; } .tblLogin{ margin-top : 50px; border:red 3px solid; } .textbox_detail { font-size:18px; text-indent:5px; background:azure; border:blue 1px solid; border-radius:4px; cursor:pointer; } .btn_submit { font-size:18px; width:100px; border:blue 1px solid; background:azure; color:blue; cursor:pointer; } .blink_text { -webkit-animation-name: blinker; -webkit-animation-duration: 1s; -webkit-animation-timing-function: linear; -webkit-animation-iteration-count: infinite; -moz-animation-name: blinker; -moz-animation-duration: 1s; -moz-animation-timing-function: linear; -moz-animation-iteration-count: infinite; animation-name: blinker; animation-duration: 1s; animation-timing-function: linear; animation-iteration-count: infinite; color:red; font-size:large; } @-moz-keyframes blinker { 0% { opacity: 1.0; } 50% { opacity: 0.0; } 100% { opacity: 1.0; } } @-webkit-keyframes blinker { 0% { opacity: 1.0; } 50% { opacity: 0.0; } 100% { opacity: 1.0; } } @keyframes blinker { 0% { opacity: 1.0; } 50% { opacity: 0.0; } 100% { opacity: 1.0; } }
Creating the Login Session in PHP
in this step we are going to add logged in id and time to session variable and used PHP function to check if the login session expire time is passes.
<?php session_start(); include("functions.php"); $message=""; if(count($_POST)>0) { if( $_POST["user_name"] == "admin" and $_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:home.php"); } else { header("Location:logout.php?session_expired=1"); } } if(isset($_GET["session_expired"])) { $message = "Session is Expired. Please Login Again."; } ?>
Creating Welcome Page When Logged in
in this step we are creating a welcome page this page is visible for logged in user only.
<html> <head> <title>Create User Login Session Timeout Using PHP</title> <link rel="stylesheet" type="text/css" href="styles.css" /> </head> <body> <table border="0" cellpadding="10" cellspacing="1" width="100%" class="tblLogin"> <tr class="tableheader"> <td align="center">Home</td> </tr> <tr class="tablerow"> <td align="center"> <p class="blink_text"> <?php if(isset($_SESSION["user_name"])) { ?> Welcome <?php echo $_SESSION["user_name"]; ?>. <?php } ?> </p> </td> </tr> <tr class="tableheader"> <td align="center"> Click here to <a href="logout.php"> Logout </a> </td> </tr> </table> </body> </html>
Checking Login Session Timeout in PHP
in this step we are use PHP Funtion to check login session timeout .
<?php session_start(); $message=""; if(count($_POST)>0) { if( $_POST["user_name"] == "admin" and $_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:home.php"); } ?>
Creating Session Expire
in this step we are going to create session expiration using PHP.
<?php session_start(); include("functions.php"); if(isset($_SESSION["user_id"])) { if(isLoginSessionExpired()) { header("Location:logout.php?session_expired=1"); } } ?>
If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.