2012-06-08 61 views
1

我今天正在建设我的网站,我意识到每次我去测试它,我必须保持登录到我的网站我的帐户,这将是正常的,除非我设置Cookie过期在30天内,出于某种原因,我认为他们没有正确地做好自己的工作,但不幸的是,我并没有很多关于解决问题的知识,下面是在登录时设置cookie的代码,如果您需要更多信息,请告诉我。不保存帐户登录的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 = ''; 
} 

感谢您的任何和所有帮助

+1

请正确缩进您的代码,您的for循环在哪里开始?未格式化的代码使你看起来很粗心 –

回答

2

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

time() + 60*60*24*100 

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

更新

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

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

+0

我的意思是我必须保持登录状态,每当我关闭浏览器并重新打开它时 – Arken

+0

@Arken正确,我更新了我的答案以提及会话 - 当您关闭浏览器时可能会过期。 ..所以你必须使用饼干重新填充它 –

+0

好的谢谢你的帮助 – Arken

0

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

PHP Session timeout

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

+0

它不会立即超时5-10分钟 – Arken

+0

尝试明确设置会话超时,然后,如链接所示,例如30分钟。 –

1

会话到期和cookie到期是不同的事情。您不应将PHP会话设置为30天,默认值为1小时(也许30分钟)。你需要的是一种将用户登录到网站并拥有特殊cookie的自动登录(重启PHP会话)的方式。