Cookie不保持帐户登录

时间:2012-06-08 17:06:17

标签: php html

我今天继续构建我的网站,我意识到每次我去测试它,我必须在我的网站上登录我的帐户,这是正常的,除了我设置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 = '';
}

感谢您提供的任何帮助

3 个答案:

答案 0 :(得分:2)

我的代码中没有太多错误,除了:

time() + 60*60*24*100

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

<强>更新

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

请参阅PHP Session timeout

您是否看到会话超时,比如20分钟不活动(而不是您希望的30天),或者如果您登录,请立即超时,关闭浏览器,打开浏览器,然后尝试再次访问该网站?