如何进行自动登录,将会话存储在浏览器中 [英] How to do auto login, store the session in the browser

查看:53
本文介绍了如何进行自动登录,将会话存储在浏览器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到过一些社交网站,如果你有用户账号,打开浏览器输入url,即使关闭电脑重启也能直接登录,不用输入用户名和密码浏览器您仍然可以自动登录,这是不同的形式,您需要使用您的登录详细信息登录,我认为这不仅包括如下所示的会话cookie

I have seen some social network sites, if you have a user account, and when you open the browser and type the url, you will be directly loged in without inputting the username and password even if you close the computer and restart the browser your can still be automatically loged in which is different form before which you need to log in with your log in details, I think this is not only include the session cookie like the following

    setcookie(session_name(), '', time()-2592000, '/');

但可能比它更复杂.假设如果我使用php,谁能告诉我如何实现这个功能,我假设它是在服务器端完成的,如果我没有说明清楚,抱歉.

but might be more complicated than it. suppose if I use php, can anybody tell me how to implement this function, I assume it's done on the server side, sorry if I didn't make clear description.

推荐答案

我所做的是,我将用户 ID 和/或用户名和登录哈希,md5(用户 ID + 用户名 + 密码)作为 cookie.

What I do is, I keep user ID and/or username and login hash, md5(userID + username + password) as cookies.

再次访问时,从 cookie 中获取 ID 并根据生成的相同公式检查登录哈希.如果相同,则自动登录.

Upon another visit, take ID from cookie and check login hash against the same formula it was generated. If the same, login automatically.

全球

$randomSeperator = '!~!';

第一次运行...

$hash = md5($id . $randomSeperator . $username);
setCookie('id', $id);
setCookie('username', $username);
setCookie('hash', $hash);

第二次运行...

$id/$username/$hash = $_COOKIE[][][] .... // Get all 3 cookies

if($hash == md5($id . $randomSeperator . $username) ){
// Do Autologin
}

这篇关于如何进行自动登录,将会话存储在浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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