为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 使用 cookie 登录会话

PHP 使用 cookie 登录会话

PHP
慕雪6442864 2023-07-15 15:44:14
请看一下这段代码。是否可以使用cookies来注册用户if (isset($_COOKIE['rand_nm']) && isset($_COOKIE['token'])) {                        $start_date = date("Y-m-d h:i:sa");                        $stmt = $con->prepare("SELECT * From tbl_token Where username = ? AND selector_hash = ?");            $stmt->execute(array($_COOKIE['rand_nm'], $_COOKIE['token']));            $row = $stmt->fetch();            $count = $stmt->rowCount();                        if($row["expiry_date"] >= $start_date) {                $isExpiryDareVerified = true;            }                        if ($_COOKIE['rand_nm'] == $row['username'] && $_COOKIE['token'] == $row['selector_hash'] && $isExpiryDareVerified) {                if ($count > 0) {                $_SESSION['userName'] = $row['username'];                $_SESSION['id'] = $row['id'];                }            }}提交表单时处理表单数据并更新数据库表,然后存储cookies信息。数据库中的令牌[随机数]和用户名。登录后...    if ($_SERVER['REQUEST_METHOD'] == 'POST') {                        if (isset($_POST['login'])) {                $user = $_POST['username'];                $pass = $_POST['password'];                $hashPass = sha1($pass);                                if (empty($_POST['username']) || empty($_POST['password'])) {                    header('Location: signup.php?error=fieldsempty');                    exit();                                    }                 
查看完整描述

1 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

这是可能的,但我不建议你这样做。特别是在 cookie 中存储可能未加密的密码。


您可以在 cookie 中存储会话 ID,并将该代码存储在数据库中。这样您就可以“记住”谁通过该特定浏览器使用了您的网站,以及用户是否已注销。现在,您无需将未加密的敏感信息存储在易于访问的 cookie 中。


if(isset($_COOKIE['sessionid']) {

    //looking for that session id in the database here... your object is $session filled data from the database.

    if($session->stillLogged()) {

        //Authenticate the user..

    }

}


查看完整回答
反对 回复 2023-07-15
  • 1 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信