无法在CodeIgniter中调用会话变量

时间:2016-07-14 16:37:19

标签: php mysql codeigniter

我试图在登录后访问会话变量但会话变量中没有显示但登录和注销功能运行良好我也在模型部分使用*(全选)。 控制器部分

<?php
class login extends CI_Controller
{
        function __construct() {
        parent::__construct();
        $this->load->helper('url');     
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');


}



    function index()
    {


        $data['main_content'] = 'index';
        $this->load->view('includes/template', $data);
    }


    function login_form(){


        if($this->session->userdata('logged_in')==1)
    {
        $this->index();
    }
    else
    {
        $data['main_content'] = 'login_form';

        $this->load->view('includes/template', $data);

    }

    }


    function validate_credentials()
{


        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');

        if($this->form_validation->run())
        {
            $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query)
        {
            //$data = array('email'=>$this->input->post('email'), 
            //'is_logged_in' => true );

            $this->load->helper('url');
            $this->load->library('session');
            $this->session->set_userdata(

            array(
            'id'     =>$user_details->id,
            'email'     =>$user_details->email,
            'name'     =>$user_details->name,

            'logged_in'=>1));
            redirect('login/index');

        }
        else
        {

        $this->session->set_flashdata('login_failed', 'invalid username/password.');
        $this->login_form();

        }

        }

else
{
    $this->login_form();
}
}








    function logout(){
        $this->session->sess_destroy();
        $this->index();
    }

}
?>

模型部分

<?php
class membership_model extends  CI_Model
{
    public function __construct() 
     {
           parent::__construct(); 
           $this->load->database();
     }

    function validate($data)
    {


        $this->db->select('*');
        $this->db->from('register');
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', $this->input->post('password'));
        $query= $this->db->get();

        if($query->num_rows == 1)
        {
            return true;
        }
        else
        {
            return false;
        }   

    }



}

?>

1 个答案:

答案 0 :(得分:0)

控制器:

首先,无需再次添加网址和会话库

第二名:

其中u在控制器

中定义$ user_details变量
$this->session->set_userdata(
    array(
    'id'=>$user_details->id,
    'email'=>$user_details->email,
    'name'=>$user_details->name,
    'logged_in'=>1
 ));

型号: num_rows应该与num_rows()

相同
   if($query->num_rows() == 1)
    {
        return true;
    }
    else
    {
        return false;
    }