检查是否已从另一个控制器重定向

时间:2015-03-12 13:05:30

标签: codeigniter

在我的登录控制器或视图上,我希望能够查看是否可以检查页面是否已重定向回登录,然后显示引导错误消息。我还在codeigniter中使用了MY_controller函数

我不想使用codeigniter会话flash数据消息。我在代码中显示了自己的错误消息。

是否可以检查控制器是否已从另一个控制器重定向?

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {

    private $error = array();

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
    }

    public function index() {        
        $data['title'] = 'Administration';

        $user_id = $this->session->userdata('user_id');

        if (isset($user_id)) {
            $this->error['warning'] = "Working";
        } else {
            $this->error['warning'] = "";
        }

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        $this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run($this) == FALSE) {

            $this->load->view('template/common/login.tpl', $data);

        } else {

            redirect('admin/dashboard');

        }

    }

    public function validate() {

        $this->load->library('user');

        if ($this->user->login() == FALSE) {

            $this->form_validation->set_message('validate', 'Does not match any of our database records');

            return false;

        } else {

            return true;
        }
    }
}

MY Controller

<?php 

class MY_Controller extends MX_Controller {

    public function __construct() {
        parent::__construct();

        Modules::run('admin/error/permission/check');
    }
}

更新我试过这仍然显示消息,即使没有从另一个页面重定向。

1 个答案:

答案 0 :(得分:0)

感谢@AdrienXL的一些好建议。 $this->load->library('user_agent');是最好的方法。

if ($this->agent->referrer()) {
    $this->error['warning'] = "Working";
} else {
        $this->error['warning'] = "";
}

<强>控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {

    private $error = array();

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
    }

    public function index() {        
        $data['title'] = 'Administration';

        $this->load->library('user_agent');

        if ($this->agent->referrer()) {
            $this->error['warning'] = "Working";
        } else {
            $this->error['warning'] = "";
        }

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        $this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run($this) == FALSE) {

            $this->load->view('template/common/login.tpl', $data);

        } else {

            redirect('admin/dashboard');

        }

    }

    public function validate() {

        $this->load->library('user');

        if ($this->user->login() == FALSE) {

            $this->form_validation->set_message('validate', 'Does not match any of our database records');

            return false;

        } else {

            return true;
        }
    }
}