2016-01-21 54 views
0

我對Codeigniter有點新,所以我需要一些幫助,如果你可以在這裏發佈答案。謝謝。如何將userid設置爲codeigniter中的會話數組?

這裏是我的控制器:

public function login() 
{ 

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

    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|min_length[3]'); 
    $this->form_validation->set_rules('passwd', 'Passwd', 'required|min_length[3]'); 

    $data = array(); 

    if ($this->form_validation->run()) 
    { 
     $email = $this->input->post('email'); 
     $passwd = $this->input->post('passwd'); 

     $this->load->model('user_model'); 

     if ($this->user_model->check($email, $passwd)) 
     { 

      $this->session->set_userdata('user', $email); 

      redirect(base_url('/')); 
     } 
     else 
     { 
      $data['error'] = 'Wrong e-mail or password.'; 
     } 
    } 

    $this->load->view('login', $data); 
} 

,這裏是我的型號:

public function __construct() 
{ 
    // Call the CI_Model constructor 
    parent::__construct(); 
} 

public function check($email = NULL, $password = NULL) 
{ 
    $this->db->select('id'); 
    $this->db->where('email', $email); 
    $this->db->where('password', sha1($password)); 
    $this->db->limit(1); 
    $result = $this->db->get('php_users_login'); 

    return ($result->num_rows() == 1); 
} 

我希望把用戶ID到會話(數據庫結構像ID,電子郵件,密碼...)

謝謝你的幫助!

回答

2

試試這個

型號

$this->db->select('id'); 
$this->db->where('email', $email); 
$this->db->where('password', sha1($password)); 
$this->db->limit(1); 
$result = $this->db->get('php_users_login'); 
return $result->row(); 

控制器

$checkData=$this->user_model->check($email, $passwd); 
if(count($checkData)==1){ 
    $this->session->set_userdata('user_id', $checkData->id); 
} 
+0

你最歡迎@gazrobur –

+0

我有正確的尤爾可變http://stackoverflow.com/posts/34921382/revisions – devpro

+1

非常感謝你@devpro –

0

試試這個

在控制器

public function login() 
{ 

    $this->load->library('form_validation'); 
    $this->load->model('user_model'); 

    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|min_length[3]'); 
    $this->form_validation->set_rules('passwd', 'Passwd', 'required|min_length[3]'); 

    $data = array(); 

    if ($this->form_validation->run()) 
    { 
     $email = $this->input->post('email'); 
     $passwd = $this->input->post('passwd');   

     $result = $this->user_model->check($email, $passwd); 

     if ($result == true) 
     { 
      $newdata = array(
       'email'  => $email, 
       'logged_in' => TRUE 
      ); 

      $this->session->set_userdata($newdata); 

      $this->load->view('home', $data); # load view 
     } 
     else 
     { 
      $data['error'] = 'Wrong e-mail or password.'; 
      $this->load->view('login', $data); 
     } 
    } 
    else{ 
     echo "Form validation Error"; 
    } 

} 

在模型

public function check($email, $password) 
{ 

    $query = $this->db->query("SELECT * FROM php_users_login WHERE email = '$email' AND password = sha1('$password') "); 
    $result = $query->result_array(); 

    $count = count($result); 

    if (empty($count) || $count > 1) { 
     return false; 
    } 
    else{ 
     return true; 
    } 
} 
+0

這給了我別的語句(表單驗證錯誤) – gazrobur

0

可以在會話中設置用戶ID爲:

使用此模型中的:

public function check($email = NULL, $password = NULL) 
{ 
    $userid = 0; 
    $this->db->select('id'); 
    $this->db->where('email', $email); 
    $this->db->where('password', sha1($password)); 
    $this->db->limit(1); 
    $result = $this->db->get('php_users_login'); 

    if($result->num_rows() == 1){ 
     $data = $query->result_array(); 
     $userid = $data[0]['id']; 
    } 
    return $userid; 
} 

在控制器:

// if login successfull 
$this->load->model('user_model'); 
$userid = $this->user_model->check($email, $passwd); 
if ($userid > 0) 
{ 
    // session array 
    $session_data = array(
      'session_userid' => $userid,  
      'session_email' => $email 
      ); 

    $this->session->set_userdata($session_data); 
    redirect(base_url('/')); 
} 
else 
{ 
    $data['error'] = 'Wrong e-mail or password.'; 
} 

側面說明:

我沒有看到會議庫包含在您的代碼,請確保您使用的是autoload.php或包含在同一個控制器。

$this->load->library('session'); 
相關問題