php - 几分钟后如何销毁 session

标签 php mysql database session cookies

大家好,我如何在几分钟后(例如 30 分钟)销毁 session ,如果有人可以帮助我解决这个问题,我将非常感激,

这是我用于登录的代码:

检查登录:

 <?php
// checkLogin.php

session_start(); // Start a new session
require('db.php'); // Holds all of our database connection information

// Get the data passed from the form
$username = $_POST['user'];
$password = $_POST['pass'];

// Do some basic sanitizing
$static_salt='asdfasdfqwertyuiop123ABC_some_static_salt_string';
$username = stripslashes($username);
$password = stripslashes($password);
$password=hash('sha512', $password . $static_salt . $username);


$sql = "select * from users where user = '$username' and pass = '$password'";
$result = mysql_query($sql) or die ( mysql_error() );

$count = 0;

while ($line = mysql_fetch_assoc($result)) {
     $count++;
}

if ($count == 1) {
     $_SESSION['loggedIn'] = "true";
     header("Location: index.php"); // This is wherever you want to redirect the user to
} else {
     $_SESSION['loggedIn'] = "false";
     header("Location: deshtoi.php"); // Wherever you want the user to go when they fail the login
}

?>

登录.php

 <div class="login-form">
<form action="checkLogin.php" method="post">
<input class="fusha" placeholder="Llogaria" type="text" name="user">
<input class="fusha" placeholder="Fjalekalimi" type="password" name="pass">
<input class="fusha" style="width:272px;" type="submit" value="Kycu"/>
</form>

索引.php

<?php session_start();
if ($_SESSION['loggedIn'] != "true") {
     print('<script>window.location = "login.php"</script>');
}
?>

最佳答案

一种解决方案是自行实现超时,如以下帖子所述:https://stackoverflow.com/a/1270960/1688441

有关更多理论细节,请参阅完整帖子。

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}

上述的另一种变体是:

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_destroy();
    die("Your session has expired");
}

关于php - 几分钟后如何销毁 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25179182/

相关文章:

mysql - 根据列中生日与当前日期的差异,根据年龄从数据库中选择记录

MySQL:选择一定时期内具有一定数量的条目

mysql - rails 3 : Saving HABTM association as array in DB?

java - 数据库操作后如何刷新listView

PHP foreach删除数组元素

php - 通过 PHP 执行 Linux 命令

php - 如何在 ActionScript 3 中获取关于 MySQL 时间戳的时间?

php - fatal error : Class 'JDatabaseDriver' not found

php - 使用多个条件更新 cakephp 中的查询

python - 在列中,计算逗号分隔句子中的单词