Codeigniter 会话从头视图文件检查

发布于 2024-11-08 04:42:09 字数 760 浏览 0 评论 0原文

我是个新手,刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-11-15 04:42:09

首先,你没有理由不能在你的视图中编写这样的代码:

<? echo ($this->session->userdata('loggedin')) ? "Logged In": "Not Logged In"; ?>

那么你的控制器就不需要任何代码。

但是,如果检查更复杂或其他什么,那么您可以在几个地方进行检查。

1) 在自定义控制器的构造函数中:创建一个新文件 application/core/MY_Controller.php,并使用以下内容覆盖构造函数:

class MY_Controller extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
        if($this->session->userdata("loggedin")) {
            // do something
        }
    }
}

然后使所有控制器扩展 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:

<? echo ($this->session->userdata('loggedin')) ? "Logged In": "Not Logged In"; ?>

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:

class MY_Controller extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
        if($this->session->userdata("loggedin")) {
            // do something
        }
    }
}

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/

—━☆沉默づ 2024-11-15 04:42:09

我认为在控制器上使用构造将是最明智的方法。

我还建议对您的会话 cookie 进行加密。


class Blog extends CI_Controller {

       public function __construct()
       {
            parent::__construct();
            //always check if session userdata value "logged_in" is not true
            if(!$this->session->userdata("logged_in"))
            {
                 redirect('login');
            }

       }
}

I think using a construct on your controller would be the smartest approach.

I also recommend encrypting your session cookie.


class Blog extends CI_Controller {

       public function __construct()
       {
            parent::__construct();
            //always check if session userdata value "logged_in" is not true
            if(!$this->session->userdata("logged_in"))
            {
                 redirect('login');
            }

       }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文