Good Day!!!
In this article, we are going to learn on
How To Create User Login Logout Session Timeout Using PHP. This user login is a very common tutorial for us. In this case, we are going to add a function.
In the user login, we set a session expiration time when the user logged-in. If the account of user elapsed to the set time then the user will no longer access his/her account and it will automatically log out after refreshing the page.
HTML Source Code
This HTML source code is to show the login form for the user.
<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">
<td align="center" colspan="2">Login Details
</td>
<td align="right">Username
</td>
<td><input type="text" autofocus="autofocus" class="textbox_detail" name="user_name"></td>
<td align="right">Password
</td>
<td><input type="password" autofocus="autofocus" class="textbox_detail" name="password"></td>
<td align="center" colspan="2"><input type="submit" class="btn_submit" name="submit" value="Login"></td>
PHP Script
This PHP script is used to check the session account of the user if the login session expiration time is reached.
<?php
include("functions.php");
$message="";
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()) {
} else {
header("Location:logout.php?session_expired=1");
}
}
if(isset($_GET["session_expired"])) {
$message = "Session is Expired. Please Login Again.";
}
?>
Our tutorial look like this:
This is the login form.

This is our Home Page.

If the user session is a timeout.

So what can you say about this work? Share your thoughts in the comment section below or email me at
[email protected]. Practice Coding. Thank you very much.