Breaking

Tuesday, 8 August 2017

How To Create User Login Session Timeout Logout In PHP

Let take a tour on how to create a simple user login session timeout logout in php,this functionality is to set the time in which user login session will expire and the user will be logged out automatically from the website.At times the essence of this , is mainly for security purpose to avoid other from getting access into your account.
We will take it in more simplified way for your better understanding,just follow the steps in a known time the functionality will be created.

how-to-import-mysql-using-php.jpg
Steps To Create User Login Session Timeout Logout In PHP


1. Create a script with php and name it as functions.php with the below code
<?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;

}

?>

This help in setting the session during,once the duration is exceeded the user will be logged out automatically from the website.
2. Create style to give our page more beautiful look with the below code and name 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 and name it user_login_session.php with the given code below
<?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");

}

?>

<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 create user logout session with the code below and name 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 help us to destroy the session when it has expired and pull out the user from being logged into the website.
5. Next follow by creating the user dashboard and name 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 here to <a href="logout.php" tite="Logout">Logout.

<?php

}

?>

</td>

</tr>

</body>

</html>


6. Finally create an index.php code with the given code below
<?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: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 in 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 have built a simple user login session timeout in PHP

   

Get our updates delivered to your inbox



1 comment:

  1. WĐžnderful article! We are linking to this great content on our website.
    Keep up the good writing.

    ReplyDelete

Want to get all latest updates from DbencoPlanet.com?

Subscribe to DbencoPlanet to get all the latest updates free!