如何设置会话超时?
destroy session after timeout and want to update database table when session time is over
这是HTML代码
名称:
地址:
街道:
电话:
</form>
**php Code**
<?php
session_start();
//set session timeout
$timeout = 10;
include 'conn.php';
if(isset($_POST['s1']))
{
$_SESSION['timeout']=time();
echo "start time";echo $_SESSION['timeout'];echo "<br>";
$name=$_POST['t1'];
$address=$_POST['t2'];
$street=$_POST['t3'];
$phone=$_POST['t4'];
//update the query when button is set
if(!mysqli_query($con,'UPDATE test SET `Name`="Hold1" where `Address`="chd"'))
{
echo("Error description: " . mysqli_error($con));
}
}
?>
然后使用激光会话超时检查会话超时
<?php
**if session is set then run this code**
if(isset($_SESSION['timeout']))
{
echo $_SESSION['timeout'];
echo "<br>";
**when time cross the current time destroy the session and update the query**
$duration = time() - (int)$_SESSION['timeout'];
echo $duration;echo ">";echo $timeout;
if($duration > $timeout)
{
header("location:index.php");
session_destroy();
session_start();
**update query when session is destroyed**
if(!mysqli_query($con,'UPDATE test SET `Name`="krishan" where `Address`="chd"'))
{
echo("Error description: " . mysqli_error($con));
}
else
{
echo "updated";
}
}
}
$_SESSION['timeout'] = time();
?>