codeigniter在所有控制器错误中检查会话

时间:2017-01-18 04:09:40

标签: php codeigniter

使用下面的代码我用来检查用户是否登录

if(!$this->session->userdata['logged_in'])
    {

    redirect("gt_login");
    }
    else
    {

    }

但是这段代码只在localhost中工作,而我把它放到服务器中它会显示这样的错误 error

这是我加载会话库的地方

function __construct() { parent::__construct(); $this->load->library('session'); $this->load->database(); $this->load->helper('url'); $this->load->helper('form'); $this->load->model('gt_home_content_model'); if(!$this->session->userdata['logged_in']) { redirect("gt_login"); } else { } }

我对codeigniter并不熟悉,也有任何其他方法可以做任何人建议我这样做....

6 个答案:

答案 0 :(得分:1)

尝试这样......

您的问题是使用[]来访问会话变量。 []用于访问数组元素。会话变量不能是数组。

如果要验证会话值是否存在。请使用$this->session->has_userdata('some_name');

if(!$this->session->has_userdata('logged_in')['username'])
{
  redirect("gt_login",'refresh');
}
else
{

}

答案 1 :(得分:0)

请在使用前加载会话库。在autoload.php中

$autoload['libraries'] = array('session');

之后,您可以在控制器视图和模型中使用会话方法。

$this->session->set_userdata('logged_in','Nishant');// set session data
$this->session->userdata('logged_in'); // using session data

答案 2 :(得分:0)

验证成功后,您必须在登录控制器中设置会话数据。像:

$this->session->set_userdata('logged_in', TRUE);

然后你可以在你的构造函数中调用它。

答案 3 :(得分:0)

<?php 

after loding session library
$this->load->library('session');


you can set the session like this in login your login controller 

class Login_controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('login_model');
    }

    public function userLogged() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $sdata=array();
            $sdata['name'] = $session_data['name'];
            $this->session->set_userdata($sdata);
            redirect('admin_controller/index');          
        } else {
            //If no session, redirect to login page
            redirect('login_controller/index', 'refresh');
        }
    }
}

now you can check user is logged in or not 
in another controller look like this code 

<?php  class Another_controller extends CI_Controller{
    public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('logged_in')) {
            redirect('login_controller/index');
        }
    }
}

?>

答案 4 :(得分:0)

这是我使用

的登录控制器
public function user_login_process()
{

    $this->form_validation->set_rules('username', 'username', 'trim|required');
    $this->form_validation->set_rules('password', 'password', 'trim|required');


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

redirect('LoginController/index');
} 
else
{
    $data = array('username' => $this->input->post('username'),'password' => md5($this->input->post('password')));
    $result = $this->gt_login_model->login($data);
    if ($result == true)
    {
        $username = $this->input->post('username');
        $result = $this->gt_login_model->read_user_information($username);
        if ($result != false) 
        {
        $session_data = array('username' => $result[0]->user_name);
        // Add user data in session
        $this->session->set_userdata('logged_in', $session_data);
        $this->load->view('admin/intropage');
        }
    } 
    else
    {
    $data = array('error_message' => 'Invalid Username or Password');
    $this->load->view('admin/login/login_dashboard', $data);
    }
}
}

答案 5 :(得分:0)

set user_login_process look like this function


 public function userLoginCheck() {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'email', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
    if ($this->form_validation->run() == FALSE) {
        $sdata = array();
        $sdata['error'] = '<font color="red">Invalid Email or Password </font>';
        $this->session->set_userdata($sdata);
        redirect("login_controller/index");
    } else {
            //Go to private area
        redirect('login_controller/userLogged', 'refresh');
    }
}