Codeigniter 会话从头视图文件检查
我是个新手,刚刚开始使用 Codeigniter,并且遇到了一些关于会话的困惑。
我想要实现的是,就像在常规 php 中一样,我想通过使用检查会话数据的标头包含文件来检查用户是否登录。我不想在将数据传递到视图文件时在每个控制器中检查/写入检查代码。
有人可以告诉我如何做到吗?
前任。我不想在每个控制器中执行以下操作:
//Controller:
if($this->session->userdata('loggedin'){
$data['loggedin'] = $this->session->userdata('loggedin');
}
//I dont want to check the above on every function in every controller
$this->load->view('some_view_file', $data);
//some_view_file
if(isset($loggedin)){
echo "You are logged in!";
}
else
{
echo "Please log in!";
}
相反,我想要类似以下内容的内容:
//some view file
if(isset($loggedin))
{
echo "your logged in";
}
else
{
echo "please log in";
}
另外,如何使用本机 php 会话而不是 CI 会话。任何帮助将不胜感激。谢谢。
I am fairly new and just started to use Codeigniter, and have come across some confusion regarding sessions.
What I want to achieve is, like in regular php, I want to check if a user is logged in by using a header include file which checks the session data. I dont want to check/write that checking code in every controller while passing data to the view file.
Can someone please show me how it can be done?
Ex. I don't want to do the following in every controller:
//Controller:
if($this->session->userdata('loggedin'){
$data['loggedin'] = $this->session->userdata('loggedin');
}
//I dont want to check the above on every function in every controller
$this->load->view('some_view_file', $data);
//some_view_file
if(isset($loggedin)){
echo "You are logged in!";
}
else
{
echo "Please log in!";
}
Instead, I want something to like the following:
//some view file
if(isset($loggedin))
{
echo "your logged in";
}
else
{
echo "please log in";
}
And also, how can I use native php sessions instead of CI Sessions. Any help will be much appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,你没有理由不能在你的视图中编写这样的代码:
那么你的控制器就不需要任何代码。
但是,如果检查更复杂或其他什么,那么您可以在几个地方进行检查。
1) 在自定义控制器的构造函数中:创建一个新文件
application/core/MY_Controller.php
,并使用以下内容覆盖构造函数:然后使所有控制器扩展 MY_Controller。
2) 或者在
post_controller_constructor
钩子中。 http://codeigniter.com/user_guide/general/hooks.html (这更多透明,如果您已经有大量控制器,可能会更容易)您可以使用本机会话:
http://codeigniter.com/wiki/Native_session/
Firstly, theres no reason you can't just write something like this in your view:
Then your controllers don't need any of that code.
However if the check is more complex or something, then theres a few places you can do it.
1) In the constructor of a custom controller: create a new file
application/core/MY_Controller.php
, and override the constructor with something like:then make all your controllers extend MY_Controller.
2) Or in a
post_controller_constructor
hook. http://codeigniter.com/user_guide/general/hooks.html (this is more transparent, and probably easier if you have tons of controllers already)You can use native sessions with this:
http://codeigniter.com/wiki/Native_session/
我认为在控制器上使用构造将是最明智的方法。
我还建议对您的会话 cookie 进行加密。
I think using a construct on your controller would be the smartest approach.
I also recommend encrypting your session cookie.