php 如何使用 Codeigniter 会话库创建“记住我复选框”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3984313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create "remember me checkbox" using Codeigniter session library?
提问by ahmed
in Codeigniter I am building an Authentication system for my web site and to achieve that I use session library
在 Codeigniter 中,我正在为我的网站构建一个身份验证系统,并使用会话库来实现
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!?
我想在登录表单中提供一个“记住我”复选框,以便用户可以永远保存会话 - 找不到永远保存会话的方法!?
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
注意:$sess_expiration 将不起作用,因为它为所有用户设置了到期日期,而我想要做的是根据他的偏好设置到期日期
is that possible? and how to do it?
那可能吗?以及怎么做?
Thanks
谢谢
回答by Mischa
If the remember me checkbox is checked you set a cookie on the user's system with a random string. E.g.:
如果选中了记住我复选框,您将使用随机字符串在用户系统上设置一个 cookie。例如:
$cookie = array(
'name' => 'remember_me_token',
'value' => 'Random string',
'expire' => '1209600', // Two weeks
'domain' => '.your_domain.com',
'path' => '/'
);
set_cookie($cookie);
You also save this random string in the users
table, e.g. in the column remember_me_token
.
您还可以将此随机字符串保存在users
表中,例如列中remember_me_token
。
Now, when a user (who is not yet logged in) tries to access a page that requires authentication:
现在,当用户(尚未登录)尝试访问需要身份验证的页面时:
- 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
- 你通过
remember_me
他的系统上的令牌名称检查是否有cookie - 如果存在,则检查数据库是否存在具有相同值的记录
- 如果是这样,您重新创建此用户的会话(这意味着他们已登录)
- 显示他们正在访问的页面
If one of the requirements above is not met, you redirect them to the login page.
如果不满足上述要求之一,则将它们重定向到登录页面。
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.
出于安全原因,您可能希望在remember_me_token
每次用户登录时更新随机数。您还可以在每次用户登录时更新 cookie 的到期日期。这样他将保持登录状态。
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.
为您编写所有代码的工作量太大,但我希望这有助于您自己实现此功能。如果您有任何问题,请发表评论。祝你好运。
回答by Fer
I needed the same thing. You cannot accomplish this using CI settings so I have chosen to override the setcookie method of the CI Session class (in MY_Session):
我需要同样的东西。您无法使用 CI 设置完成此操作,因此我选择覆盖 CI Session 类(在 MY_Session 中)的 setcookie 方法:
function _set_cookie($cookie_data = NULL)
{
if (is_null($cookie_data))
{
$cookie_data = $this->userdata;
}
// Serialize the userdata for the cookie
$cookie_data = $this->_serialize($cookie_data);
if ($this->sess_encrypt_cookie == TRUE)
{
$cookie_data = $this->CI->encrypt->encode($cookie_data);
}
else
{
// if encryption is not used, we provide an md5 hash to prevent userside tampering
$cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
}
setcookie(
$this->sess_cookie_name,
$cookie_data,
$this->userdata('rememberme') == true ? $this->sess_expiration + time() : 0,
$this->cookie_path,
$this->cookie_domain,
0
);
}
Of course you need to set the rememberme flag in your session based upon the choice the user made.
当然,您需要根据用户所做的选择在会话中设置 rememberme 标志。
回答by San Bergam
You could use isset to check if the cookie has a value, and if the cookie is deleted, it will return negative. If it is negative, you can set the cookie again... of course, this will be like "renewing" the cookie, so if the user doesn't come by the expiration, they will be forgotten. (Set a very big time till the expiration!)
您可以使用 isset 来检查 cookie 是否有值,如果 cookie 被删除,它将返回负值。如果是负数,你可以重新设置cookie...当然,这就像“更新”cookie,所以如果用户在到期时没有来,他们就会被遗忘。(设置一个非常大的时间直到到期!)
For example:
例如:
<?php
if (isset($_COOKIE['autologin'])) { //Checks for cookie
echo "Welcome back!... Logging you in.";
}else{ //Sets a cookie
echo "Please sign in";
$expiretime=time()+60*60*24*365; //set expire time to a year
setcookie("autologin", "What is here is not important.", $expiretime);
};
?>
回答by Danilo Colasso
I used hooks to do this
我用钩子来做到这一点
Enable hooks in "config.php" setting
$config['enable_hooks'] = TRUE;
In your login controller save the checkbox value on session
$this->session->set_userdata('remember_me', $this->input->post('remember_me'));
In your "hooks.php" fileadd this
$hook['post_controller_constructor'] = array( 'class' => 'Change_Config', 'function' => 'change_session_expiration', 'filename' => 'change_config.php', 'filepath' => 'hooks' );
In your "hooks" foldercreate a file called "change_config.php" and paste this
<?php class Change_Config { public function change_session_expiration() { $ci = & get_instance(); $remember = $ci->session->userdata('remember_me'); if($remember && $ci->session->sess_expire_on_close) { $new_expiration = (60*60*24*365); //A year milliseconds $ci->config->set_item('sess_expiration', $new_expiration); $ci->config->set_item('sess_expire_on_close', FALSE); $ci->session->sess_expiration = $new_expiration; $ci->session->sess_expire_on_close = FALSE; } } }
在“config.php”设置中启用钩子
$config['enable_hooks'] = TRUE;
在您的登录控制器中保存会话中的复选框值
$this->session->set_userdata('remember_me', $this->input->post('remember_me'));
在你的“hooks.php”文件中添加这个
$hook['post_controller_constructor'] = array( 'class' => 'Change_Config', 'function' => 'change_session_expiration', 'filename' => 'change_config.php', 'filepath' => 'hooks' );
在您的“hooks”文件夹中创建一个名为“change_config.php”的文件并粘贴它
<?php class Change_Config { public function change_session_expiration() { $ci = & get_instance(); $remember = $ci->session->userdata('remember_me'); if($remember && $ci->session->sess_expire_on_close) { $new_expiration = (60*60*24*365); //A year milliseconds $ci->config->set_item('sess_expiration', $new_expiration); $ci->config->set_item('sess_expire_on_close', FALSE); $ci->session->sess_expiration = $new_expiration; $ci->session->sess_expire_on_close = FALSE; } } }
回答by Harendra Kumar
You can just put this
你可以把这个
if ($remember) {
$new_expiration = (60*60*24*365);
$this->config->set_item('sess_expiration', $new_expiration);
$this->config->set_item('sess_expire_on_close', FALSE);
$this->session->sess_expiration = $new_expiration;
$this->session->sess_expire_on_close = FALSE;
}
回答by Jacopo
$this->session->sess_expiration = '14400'; // expires in 4 hours
$this->session->set_userdata($data); // set session
回答by Herr
You can set sess_expiration to 0 and then set a custom variable into the session like, remember_me=1. Check this value and destroy the session if needed after a time. This would be a workarround.
您可以将 sess_expiration 设置为 0,然后在会话中设置一个自定义变量,例如,remember_me=1。检查此值并在一段时间后根据需要销毁会话。这将是一个解决方法。