PHP会话登录并记住我 [英] PHP Sessions Login with remember me

查看:205
本文介绍了PHP会话登录并记住我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了一个使用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会话登录并记住我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆