1

我今天继续建立我的网站,我意识到每次我去测试它时,我都必须继续登录我网站上的帐户,这很正常,除非我将 cookie 设置为在 30 天内过期,出于某种原因我不这样做'认为他们没有正确地完成他们的工作,不幸的是我没有很多关于他们的知识来解决问题,这里是在登录时设置 cookie 的代码,如果您需要更多信息,请告诉我.

$encryptedID = base64_encode("g4enm2c0c4y3dn3727553$id");
setcookie("idCookie", $encryptedID, time()+60*60*24*100, "/"); // Cookie set to expire in about 30 days
setcookie("passCookie", $pass, time()+60*60*24*100, "/"); // Cookie set to expire in about 30 days

上面还有一些代码(可能有帮助)

if($login_check > 0){ 
    while($row = mysql_fetch_array($sql)){
        // Pleae note: Adam removed all of the session_register() functions cuz they were deprecated and
        // he made the scripts to where they operate universally the same on all modern PHP versions(PHP 4.0  thru 5.3+)
        // Create session var for their raw id
        $user_id = $row["user_id"];   
        $_SESSION['user_id'] = $user_id;
        // Create the idx session var
        $_SESSION['idx'] = base64_encode("g4p3h9xfn8sq03hs2234$id");
        // Create session var for their username
        $login_username = $row["login_username"];
        $_SESSION['login_username'] = $login_username;
        // Create session var for their password
        $login_userpass = $row["login_password"];
        $_SESSION['login_userpass'] = $login_userpass;
        $sql_login = mysql_query("SELECT no_of_logins FROM users WHERE user_id='$user_id'");
        $array = mysql_fetch_assoc($sql_login);
        $no_of_logins = $array['no_of_logins'];
        //$sql_login_check = mysql_num_rows($sql_login);
        if($no_of_logins == "0"){
            mysql_query("UPDATE users SET first_login=now() WHERE user_id='$user_id' LIMIT 1");
        }
        mysql_query("UPDATE users SET last_login=now() WHERE user_id='$user_id' LIMIT 1");
        mysql_query("UPDATE users SET online = '1' WHERE user_id='$user_id' LIMIT 1");
        mysql_query("UPDATE users SET no_of_logins = no_of_logins + 1 WHERE user_id='$user_id' LIMIT 1");
        mysql_query("UPDATE system SET total_logins = total_logins + 1");
        mysql_query("UPDATE system SET no_online = no_online + 1");
    } // close while
    // Remember Me Section
    $encryptedID = base64_encode("g4enm2c0c4y3dn3727553$id");
    setcookie("idCookie", $encryptedID, time()+60*60*24*100, "/"); // Cookie set to expire in about 30 days
    setcookie("passCookie", $pass, time()+60*60*24*100, "/"); // Cookie set to expire in about 30 days
    // All good they are logged in, send them to homepage then exit script
    header("Location: profile.php");
    exit();
} else { // Run this code if login_check is equal to 0 meaning they do not exist
    $loginErrorMsg = "Incorrect login data, please try again";
    $errorDisplay = '';
}

感谢您的任何帮助

4

3 回答 3

2

我在您的代码中没有看到太多错误,除了:

time() + 60*60*24*100

那是未来的 100 天,而不是 30 天 :)

更新

当您关闭浏览器时,会话 cookie 过期是正常的,因为它们没有在响应标头中设置明确的过期日期。

这正是您创建持久 cookie 的原因;当会话过期(或更可能不存在)时,必须使用从这些 cookie 收集的数据填充新会话。

于 2012-06-08T17:10:05.620 回答
1

会话过期和 cookie 过期是不同的东西。您不应该将 PHP 会话设置为 30 天,默认为 1 小时(可能是 30 分钟)。如果用户来到站点并拥有特殊的 cookie,您需要一种自动重新登录用户(重新启动 PHP 会话)的方法。

于 2012-06-08T17:12:12.517 回答
0

如果我没记错的话,在配置的会话超时时间过去后,会话仍然会超时(释放有限的服务器资源......这是一件好事),即使您的 cookie 仍然存在于浏览器中。

请参阅PHP 会话超时

您是在 20 分钟不活动(而不是您希望的 30 天)之后看到会话超时,还是在您登录、关闭浏览器、打开浏览器并尝试访问该站点时立即超时再次?

于 2012-06-08T17:09:35.980 回答