Here You can find the PHP code to "Expire Session Automatically After A Valid Time Period Using PHP"
Here you can find the code please click below
login.php
<?php
session_start();
?>
<html>
<form name="form1" method="post">
<table>
<tr><td>Username </td><td><input type="text" name="text1"></td></tr>
<tr><td>Password</td><td><input type="password" name="pwd"></td></tr>
<tr><td><input type="submit" value="SignIn" name="submit1"> </td></tr>
</table>
</form>
</html>
<?php
if(@$_POST['submit1'])
{
$u = $_POST['text1'];
$p = $_POST['pwd'];
if($u =="ritu" && $p=="raj")
{
$_SESSION['luser'] = $u;
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (1 * 60) ; // here 1 mean one minute
header('Location: homepage.php');
}
else
{
echo " Enter Username or Passwod again !";
}
}
?>
homepage.php
<?php
session_start();
if(!isset($_SESSION['luser']))
{
echo "Please Login again";
echo "<a href='login.php'>Click Here to Login</a>";
}
else
{
$now = time(); // checking time now when homepage starts
if($now > $_SESSION['expire'])
{
session_destroy();
echo "Your session has expired ! <a href='login.php'>Login Here</a>";
}
else
{
?>
<html>
Welcome Dear <?php echo $_SESSION['luser'] . "<br>";
echo "<a href='logout.php'>LogOut</a>";
?>
</html>
<?php
}
}
?>
logout.php
<?php
session_start();
session_destroy();
header('Location: login.php');
?>
Here you can find the code please click below
DEMO
login.php
<?php
session_start();
?>
<html>
<form name="form1" method="post">
<table>
<tr><td>Username </td><td><input type="text" name="text1"></td></tr>
<tr><td>Password</td><td><input type="password" name="pwd"></td></tr>
<tr><td><input type="submit" value="SignIn" name="submit1"> </td></tr>
</table>
</form>
</html>
<?php
if(@$_POST['submit1'])
{
$u = $_POST['text1'];
$p = $_POST['pwd'];
if($u =="ritu" && $p=="raj")
{
$_SESSION['luser'] = $u;
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (1 * 60) ; // here 1 mean one minute
header('Location: homepage.php');
}
else
{
echo " Enter Username or Passwod again !";
}
}
?>
homepage.php
<?php
session_start();
if(!isset($_SESSION['luser']))
{
echo "Please Login again";
echo "<a href='login.php'>Click Here to Login</a>";
}
else
{
$now = time(); // checking time now when homepage starts
if($now > $_SESSION['expire'])
{
session_destroy();
echo "Your session has expired ! <a href='login.php'>Login Here</a>";
}
else
{
?>
<html>
Welcome Dear <?php echo $_SESSION['luser'] . "<br>";
echo "<a href='logout.php'>LogOut</a>";
?>
</html>
<?php
}
}
?>
logout.php
<?php
session_start();
session_destroy();
header('Location: login.php');
?>
No comments:
Post a Comment