PHP Sessions 登录并记住我 [英] PHP Sessions Login with remember me
本文介绍了PHP Sessions 登录并记住我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用 PHP Sessions 的 PHP 注册/登录系统,它运行良好,我希望用户能够记住我,然后他们永远保持登录状态,或者至少保持一周左右的状态.
Ive got a PHP Registration/Login system using PHP Sessions which is working perfectly, I want the user to be able to tick remember me and then they stay logged in forever or at least a week or something.
我猜我需要存储一个 cookie 并检查,我对我实际需要在 cookie 中存储的内容感到困惑.如果我存储用户 ID 或用户名,那么就不能有人使用假 cookie 来查看其他用户的数据吗?
Im guessing I need to store a cookie and check, I was confused at what I actually need to store in the cookie. If I store the userid or username then can't someone just use a fake cookie to look at another users data?
不胜感激.
推荐答案
我经常使用的小例子
function setSession($username,$password,$cookie=null){
// Other code for login ($_POST[]....)
// $row is result of your sql query
$values = array($username,$this->obscure($password),$row['id']);
$session = implode(",",$values);
// check if cookie is enable for login
if($cookie=='on'){
setcookie("your_cookie_name", $session, time()+60*60*24*100,'/');
} else {
$_SESSION["your_session_name"] = $session;
}
}
这篇关于PHP Sessions 登录并记住我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文