在CodeIgniter中用于身份验证时,$ this-> session-> userdata和$ this-> session-> has_userdata是否相同? [英] Is $this->session->userdata and $this->session->has_userdata same when use for authenticate in CodeIgniter?
问题描述
我使用了以下代码在CodeIgniter中进行身份验证。
I used use following code to authenticate in CodeIgniter.
if(empty($this->session->userdata('user_id'))){
redirect(base_url());
}
再次阅读文档后,我发现Codeigniter具有另一个功能。 / p>
After reading the documentation again I found the Codeigniter has another function for it.
if(!($this->session->has_userdata('user_id'))){
redirect(base_url());
}
如果两个代码相同或我的代码存在安全性问题?
If both codes are same or my code has security issues?
推荐答案
两者都是不同的功能&
第二个更好的选择是使用它,因为它会检查
user_data
是否具有user_id
键&因此可以运行更少的代码(但这是一个遗留功能,您应该使用isset($ _ SESSION [$ key])
代替它。)
The second is better option to use because it checks whether the
user_data
hasuser_id
key or not & thus runs on less code (but it is a legacy function you should useisset($_SESSION[$key])
instead of it).
第一个:-
if(empty($this->session->userdata('user_id'))){
redirect(base_url());
}
它访问 user_id
键的值在会话
数组的 userdata
数组中。
It access the value of user_id
key in userdata
array in session
array.
第二个:-
if(!($this->session->has_userdata('user_id'))){
redirect(base_url());
}
它检查 user_id
键是否存在
注意:
has_userdata($键)
是一种旧方法,仅保留用于与旧应用程序向后兼容。它只是isset($ _ SESSION [$ key])
的别名-请改用它。
has_userdata($key)
is a legacy method kept only for backwards compatibility with older applications. It is just an alias forisset($_SESSION[$key])
- please use that instead.
如果指定的键存在,否则返回FALSE
It returns TRUE if the specified key exists, FALSE if not
这篇关于在CodeIgniter中用于身份验证时,$ this-> session-> userdata和$ this-> session-> has_userdata是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!