Login Session Timeout & Logout In PHP

User Login Session Timeout Logout

User Login Session Timeout Logout

This PHP instructional exercise is utilized for setting client login session lapse time for the signed in the client. When this time is slipped by then the client never again gets to the confirmed pages of the application. In the past instructional exercise, we have made session factors once a client signed in to our application.

In this instructional exercise, we will include the current signed in timestamp to a session. Utilizing this timestamp we are checking if the login session termination time is come to. Provided that this is true, the client will be logged out.

Live Demo   Download Source Code

Index.php

<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" style="background-color: #afc8d6;">Username</td>
<td><input type="text" name="user_name"></td>
</tr>
<tr class="tablerow">
<td align="right" style="background-color: #73b6da;">Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" name="submit" value="Submit"></td>
</tr>
    
</table>
              <table style="margin-left: 550px;"><tr><td><span>User Name:</span> lisenme</td><td><span>Password:</span> lisenme</td></tr></table>
</form>

Style.css

.tableheader {
background-color: #1d91d0;
color:white;
font-weight:bold;
}
.tablerow {
color:#ffffff;
}
.message {
color: #333;
border: #FF0000 1px solid;
background: #FFE7E7;
padding: 5px 20px;
}

.tblLogin{
margin-top : 2px;
    width: 410px;
    margin-left: 500px;    
}

button, input {
    border: 0px;
    width: 100%;
    border-bottom: 1px solid #b1c6d2;
    overflow: visible;
}

input[type="submit"] {
    background-color: #afc8d6;
    padding: 6px;
    box-shadow: 1px 1px 3px #a2a2a2;
    border-radius: 0px;
    width: 114px;
    color: #fff;
}

input[type="submit"]:hover {
    background-color: #1d91d0;
    padding: 6px;
    box-shadow: 1px 1px 3px #a2a2a2;
    border-radius: 0px;
    width: 114px;
    color: #afc8d6;
}

function.php

<?php
function isLoginSessionExpired() {
	$login_session_duration = 10; 
	$current_time = time(); 
	if(isset($_SESSION['loggedin_time']) and isset($_SESSION["user_id"])){  
		if(((time() - $_SESSION['loggedin_time']) > $login_session_duration)){ 
			return true; 
		} 
	}
	return false;
}
?>

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");
?>

login-session.php

<?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:user_dashboard.php");
}
?>
<?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:user_dashboard.php");
}
?>

 

 

You May Also Like

About the Author: Ranjith Kumar A

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x