2015-03-24 3 views
0

Хорошо, поэтому в моей базе данных у меня есть таблица под названием «пользователи» с электронной почтой, pw и некоторыми другими полями. Регистрация работы. Но я не могу сделать Логгина вCodeIgniter loggin in

вид

Контроллер:

public function index() 
{ 

    $this->form_validation->set_rules('email2', 'Email', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('pw2', 'Hasło', 'trim|required|md5'); 

    $this->load->view('header_view'); 

    if ($this->form_validation->run() === FALSE) 
    { 
     $this->load->view('account_view'); 
    } 
    else 
    { 
     $email = $this->input->post('email2'); 
     $password = $this->input->post('pw2'); 
     $this->load->model('login_model'); 

     if($user = $this->login_model->login_user($email, $password)) 
     { 
      $this->session->set_userdata('user_id', $user['id']); 
      redirect('home'); 
     } 
     else 
     { 
      redirect('account'); 
} 

вид Модель: класс login_model расширяет CI_Model { общественности $ таблица = 'пользователей.';

public function login_user($email, $pw) 
    { 
     return $this->db->where(array('email' => $email, 'pw' => $pw))->get($this->table)->row_array();  
    } 

Вид:

<p> 
    <?php echo form_error('email2'); ?> 
    <input type="text" id="email2" name="email2" value="<?php echo set_value('email2'); ?>" /> 
    <label for="email2">Email</label> 
</p> 
<p> 
    <?php echo form_error('pw2'); ?> 
    <input type="password" id="pw2" name="pw2" /> 
    <label for="pw2">Hasło</label> 
</p> 
<p> 
    <input type="submit" value="Zaloguj" /> 
</p> 
<?php echo form_close(); ?> 
</form> 

ответ

0

В контроллере:

public function index() 
{ 

    $this->form_validation->set_rules('email2', 'Email', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('pw2', 'Hasło', 'trim|required|md5'); 

    $this->load->view('header_view'); 

    if ($this->form_validation->run() === FALSE) 
    { 
     $this->load->view('account_view'); 
    } 
    else 
    { 
      redirect('home','refresh'); 
    } 


function check_database($password) 
{ 
    //Field validation succeeded. Validate against database 
    $username = $this->input->post('email2'); 

    //query the database 
    $result = $this->user->login($email2, $password); 

    if($result) 
    { 
    $sess_array = array(); 
    foreach($result as $row) 
    { 
     $sess_array = array(
     'id' => $row->id, 
     'email2' => $row->email2 
     ); 
     $this->session->set_userdata('logged_in', $sess_array); 
    } 
    return TRUE; 
    } 
    else 
    { 
    $this->form_validation->set_message('check_database', 'Invalid username or password'); 
    return false; 
    } 
} 

В модели:

Class User extends CI_Model 
{ 
function login($username, $password) 
{ 
    $this -> db -> select('id, email, pw'); 
    $this -> db -> from('users'); 
    $this -> db -> where('email', $email2); 
    $this -> db -> where('pw', MD5($password)); 
    $this -> db -> limit(1); 

    $query = $this -> db -> get(); 

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