如何创建“记住我复选框"使用 Codeigniter 会话库? [英] How to create "remember me checkbox" using Codeigniter session library?

查看:20
本文介绍了如何创建“记住我复选框"使用 Codeigniter 会话库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Codeigniter 中,我正在为我的网站构建一个身份验证系统,并使用会话库来实现这一点

in Codeigniter I am building an Authentication system for my web site and to achieve that I use session library

     session->set_userdata('username')

这将保存会话 - 我相信 - 一段时间

this will save the session -I believe- for some time

我想在登录表单中提供一个记住我"复选框,以便用户可以永久保存会话 - 找不到永久保存会话的方法!?

I want to provide a "remember me" checkbox in the login form so the user can save the session forever - could not find a way to save the session forever!?

注意:$sess_expiration 将不起作用,因为它为所有用户设置到期日期,而我想要做的是根据他的偏好设置到期日期

Note:$sess_expiration will not work because it sets expiration date for all users and what I want to do is setting the expiration date based on his preferences

这可能吗?以及怎么做?

is that possible? and how to do it?

谢谢

推荐答案

如果记住我"复选框被选中,则您在用户系统上设置了一个带有随机字符串的 cookie.例如:

If the remember me checkbox is checked you set a cookie on the user's system with a random string. E.g.:

$cookie = array(
    'name'   => 'remember_me_token',
    'value'  => 'Random string',
    'expire' => '1209600',  // Two weeks
    'domain' => '.your_domain.com',
    'path'   => '/'
);

set_cookie($cookie);

您还将这个随机字符串保存在 users 表中,例如在 remember_me_token 列中.

You also save this random string in the users table, e.g. in the column remember_me_token.

现在,当用户(尚未登录)尝试访问需要身份验证的页面时:

Now, when a user (who is not yet logged in) tries to access a page that requires authentication:

  • 你检查他的系统上是否有名为remember_me令牌的cookie
  • 如果存在,则检查数据库是否存在具有相同值的记录
  • 如果是这样,您重新创建此用户的会话(这意味着他们已登录)
  • 显示他们正在访问的页面

如果上述要求之一不满足,您将它们重定向到登录页面.

If one of the requirements above is not met, you redirect them to the login page.

出于安全原因,您可能希望在每次用户登录时更新随机的remember_me_token.您也可以在每次用户登录时更新 cookie 的到期日期.这样他就会留下来已登录.

For security reasons you may want to renew the random remember_me_token every time the user logs in. You can also update the expiry date of the cookie every time the user logs in. This way he will stay logged in.

为您编写所有代码的工作量太大,但我希望这有助于您自己实现此功能.如果您有任何问题,请发表评论.祝你好运.

It would be too much work to write all the code for you, but I hope this helps you to implement this functionality yourself. Please comment if you have any questions. Good luck.

这篇关于如何创建“记住我复选框"使用 Codeigniter 会话库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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