首页 > 解决方案 > 如何评估构造函数codeigniter的后值?

问题描述

再会,

我有一个构造函数来检查用户是否登录..

代码:

public function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->helper('form');

    if (!logged_in()) {
        redirect('/');
        die;
    };
 }

现在我想添加另一个条件,这样即使没有登录,我仍然可以访问控制器类上的方法,就像我想传递一个密钥进行简单验证一样。

我试图通过$this->input->post('key')在构造函数中添加来获取表单值。

public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $key = $this->input->post('key');
        echo $key;
        die;

    if (!logged_in() OR $key==null) {
        redirect('/');
        die;
    };
 }

因此,如果未登录或 $key 为空,则会出现该条件,然后它将重定向到登录页面,但它始终返回空或空。我该如何正确地做到这一点?谢谢您的帮助。

标签: phpcodeigniter

解决方案


public function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->helper('form');
    $key = $this->input->post('key');

    if(!logged_in() || (logged_in() && $key=='')) {
      redirect('/');
      die;
    }
}

推荐阅读