Home >>Advance PHP Tutorial >Destroy Session after some time in PHP

Destroy Session after some time in PHP

Destroy Session after 5 minutes in PHP

By default the expiry time of any particular session that is created is 1440 secs i.e (24*60). ie 24 minutes around .

But in some cases there is necessity to change this default time .

There are two ways of doing this :

1) Either we can make changes in the php.ini file, and change the configuration .

But in this case the default time for all the sites working on that server will change.

2) There is another method to getrid of this. we can logically change the destroy time of session. here at the time of creation of session we calculate the system current time  and as the user browse to different pages of the script will check for the expiry time i.e is explicitly declared as session-expiry(time previously noted at the time of session creation + the time for which you want your session to be maintained).

The session get destroyed at that particular time and redirect the user to the homepage of that script.

index.php

 <?php

session_start();

if(@$_POST['submit1'])

{

	$u = $_POST['text1'];

	$p = $_POST['pwd'];

	if($u =="sanjeev@gmail.com" && $p=="san123")

	{

	$_SESSION['luser'] = $u;

	$_SESSION['start'] = time();

	 // taking now logged in time
	$_SESSION['expire'] = $_SESSION['start'] + (5 * 60) ; 

	// ending a session in 5  minutes from the starting time
	header('Location: HomePage.php');

	}

	else

	{

	$err= "<font color='red'>Invalid user login </font>";

	}

}

?>

<html>

<head>

	<title>Destroy Session after 5 minutes - Phptpoint.com</title>

</head>

<body>

<form name="form1" method="post">

<table align="center">

	<tr>
		<Td colspan="2"><?php echo @$err;?></Td>
	</tr>	

	<tr>

		<td>Username </td>

		<td><input type="email" name="text1"  placeholder="sanjeev@gmail.com" required>

		</td>

	</tr>

	<tr>

		<td>Password</td>

		<td><input type="password" name="pwd" placeholder="san123" required></td>

	</tr>

	<tr>

		<td colspan="2"  align="center">

		<input type="submit" value="SignIn" name="submit1">

		</td>

	</tr>

</table>

</form>

</body>

</html>

After login it redirects on home page

HomePage.php

 <?php

session_start();

if(!isset($_SESSION['luser']))

{

    echo "<p align='center'>Please Login again ";

    echo "<a href='index.php'>Click Here to Login</a></p>";

}

else

{

    $now = time();
 // checking the time now when home page starts

    if($now > $_SESSION['expire'])

    {

        session_destroy();

        echo "<p align='center'>Your session has expire ! <a href='login.php'>Login Here</a></p>";

    }

    else

    {
 //starting this else one [else1]

?>



<html>

<head>

	<title>Destroy Session after 5 minutes - Phptpoint.com</title>

</head>

<body>

<p style="background:#CCCCCC">

		Welcome <?php echo $_SESSION['luser']; ?> 

        <span style="float:right"><a href='logout.php'>LogOut</a></span>

  <p style="padding-top:20px;background:#CCCCCC; height:400px; text-align:center">

  	<span style="color:red;text-align:center">Your Session Will destroy after 5 minutes You will  
redirected on next page</span>

  <br/><br/>

  <span>if you want to logout before 5minuts click on logout link </span>

  </p>   

</p>

<?php

 }

}

?>

</body>

</html>

After 5 minutes destroy session, logout and redirect on home page.

logout.php

 <?php

    session_start();

    session_destroy();

    header('location:index.php');

?>


No Sidebar ads