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

查看:133
本文介绍了如何创建"还记得我复选框"使用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')

这将保存会话-I相信 - 有一段时间了。

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不会工作,因为它设置到期日为所有用户和我想要做的是设置根据他的preferences到期日期

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

这可能吗?怎么办呢?

感谢

推荐答案

如果该记得我复选框被选中设置在用户的系统上的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);

您还保存在用户这个随机字符串表,例如在列 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
  • 如果它的存在,你检查数据库是否存在具有相同值
  • 记录
  • 如果是这样,你重新创建用户的会话(这意味着他们登录)

  • 显示页面,他们参观

  • you check if there is a cookie by the name of remember_me token on his system
  • if it's there, you check the database if there is a record with the same value
  • if so, you recreate this user's session (this means they are logged in)
  • show the page they were visiting

如果以上的不能满足的需求之一,你重定向到登录页面。

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.

这将是工作太多了,写的所有code你,但我希望这有助于你对自己实现这个功能。如果您有任何疑问,请发表评论。祝你好运。

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天全站免登陆