我怎样才能创建一个安全的“记住我”?系统使用PHP?

发布于 2024-09-17 22:16:46 字数 89 浏览 6 评论 0原文

我有一个登录系统。如何使用 cookie 实现安全的记住我系统。

我应该在 cookie 用户名和密码中存储什么值,但如何保护它?

I have a login system. How can I implement a secure remember me system using cookies.

What value should I have to store in cookie username and password, but how I can secure it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温暖的光 2024-09-24 22:16:46

为数据库中的每个用户定义盐,然后

设置上

$expire_time = time() + 2 * 7 * 24 * 3600; // 2 weeks exp time

setcookie( 
    "rememberMe",
    crypt($username, $salt),
    $expire_time,
    '/'
);

在验证

$_COOKIE['rememberMe'] === crypt($username, $salt)

define A Salt foreach user in db then

on setting

$expire_time = time() + 2 * 7 * 24 * 3600; // 2 weeks exp time

setcookie( 
    "rememberMe",
    crypt($username, $salt),
    $expire_time,
    '/'
);

on validating

$_COOKIE['rememberMe'] === crypt($username, $salt)
一笔一画续写前缘 2024-09-24 22:16:46

也许您可以创建一个 16 个字符的字母/数字字符串,该字符串在数据库中与该用户和 MAC 地址相关联,这样(只要人们不太努力并欺骗 Mac)只有该计算机可以登录。

Maybe you could create a 16 char letter/number string that is associated in a database with that user and the mac address so that (as long as people aren't trying too hard and spoofing macs) only that machine can log on.

烟火散人牵绊 2024-09-24 22:16:46

也许您应该(在您的数据库中)存储访问者 IP、用户代理、时区或已安装的插件。使用 Javascript 可能很容易得到一些东西,因为获取 MAC 地址可能是一个问题。

然后您可以轻松检查用户是否具有与上次相同的 IP、UA、时区或插件:) 或者您可以使用 MaxMind 检查他的位置并确认他是否使用正确的时区。如果有任何可疑的情况,您应该丢弃 cookie 凭据。

Maybe you should store (in your DB) visitor IP, User Agent, time zone or installed plugins. Something that might be easy to get using Javascript, since getting MAC address might be a problem.

Then you can easily check if user has same IP, UA, time zone or plugins as last time :) Or you might use MaxMind to check his location and confirm if he is using correct time zone. If there's anything suspicious you should discard cookie credentials.

海的爱人是光 2024-09-24 22:16:46

没什么大不了的...不要让你的会话文件被清理(ini 设置 session.gc_probability = 0),并将会话 cookie 从临时更改为永久(ini 设置 session.cookie_lifetime = then_long_you_want_the_user_to_be_remembered)。

当然,您可能希望最终清理过时的会话文件,因此您可以尝试发生清理的概率非常低,或者进行一些外部清理。无论哪种方式,只要用户保留会话 cookie 并且您保留会话文件,他们就会被“记住”。

There's not much to it... don't let your session files get cleaned up (ini setting session.gc_probability = 0), and change the session cookie from temporary to permanent (ini setting session.cookie_lifetime = however_long_you_want_the_user_to_be_remembered).

Of course, you'd probably want to eventually clean up stale session files, so you could experiment with a very low probability of the cleanup occuring, or do some external cleanup. Either way, as long as the user keeps the session cookie around and you keep the session file around, they'll be "remembered".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文