2016-07-14 69 views
0

我想在登录后访问会话变量,但没有在会话变量中显示,但登录和注销功能运行良好,我也在模型部分中使用了*(全选)。 控制器部分 无法在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; 
     } 

    } 



} 

?> 

回答

0

控制器:

首先就没有必要再添加URL和会话库

第二:

其中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; 
    }