Может кто-нибудь сказать мне, почему мой check_password не работает в codeigniter

Я новичок в этом, так голый со мной.

Я пытаюсь войти с помощью verify_password. У меня была работа после того, как я обновился до php5.5, теперь он не работает. Я не знаю, что я сделал, чтобы сломать его.

Вот модель

<?php class Login_model extends CI_Model { public function __construct() { // Call the CI_Model Constructor parent::__construct(); $this -> load -> database(); } public function login($email, $password) { // SELECT id, email, password FROM user_registration WHERE email = $email & password =$password LIMIT 1 $this -> db -> select('id, email, password'); $this -> db -> from('user_registration'); $this -> db -> where('email', $email); $this -> db -> where('password', verify_password($password, 'md5')); $this -> db -> limit(1); $query = $this -> db -> get(); // IF THERE IS ONLY 1 ROW OF RESULTS THEN RETURN RESULTS. if ($query -> num_rows() == 1) { return $query -> result(); } else { return false; } } } 

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

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); public function __construct() { // Call the CI_Model Constructor parent::__construct(); //session_start(); $this->load->model('login_model'); } public function index() { // For development only //--------------------------------------------------------------- $this->output->enable_profiler(TRUE); //--------------------------------------------------------------- // load form validation library $this->load->library('form_validation'); // Validate the form $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|md5'); //$this->form_validation->set_rules('password', 'Password', 'trim|required|md5'); // if the validations were not run // display the form if($this->form_validation->run() == FALSE) { $this->load->view('html'); $this->load->view('header'); $this->load->view('navigation'); $this->load->view('login'); $this->load->view('footer'); }else{ // if the for is validated, it will be sent // to check_database to process the data and start a session // if all is ok, redirect to mypage where the session will bring up // all of the users data redirect('mypage', 'refresh'); } } function check_database($password) { //$password_matches = $this->ion_auth->hash_password_db($user->id, $old_password); //Field validation succeeded. Validate against database // $password is the posted password $email = $this->input->post('email'); // query the database, passing it the email & password // return an object $result = $this->login_model->login($email, $password); print_r($result); // get the user id //$id = $result[0]->id; // if a result was returned // trap the id in a session // else show an error message if($result){ $this->session->set_userdata('user_id', $id); }else{ $this->form_validation->set_message('check_database', 'Invalid email or password'); } } } в <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); public function __construct() { // Call the CI_Model Constructor parent::__construct(); //session_start(); $this->load->model('login_model'); } public function index() { // For development only //--------------------------------------------------------------- $this->output->enable_profiler(TRUE); //--------------------------------------------------------------- // load form validation library $this->load->library('form_validation'); // Validate the form $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|md5'); //$this->form_validation->set_rules('password', 'Password', 'trim|required|md5'); // if the validations were not run // display the form if($this->form_validation->run() == FALSE) { $this->load->view('html'); $this->load->view('header'); $this->load->view('navigation'); $this->load->view('login'); $this->load->view('footer'); }else{ // if the for is validated, it will be sent // to check_database to process the data and start a session // if all is ok, redirect to mypage where the session will bring up // all of the users data redirect('mypage', 'refresh'); } } function check_database($password) { //$password_matches = $this->ion_auth->hash_password_db($user->id, $old_password); //Field validation succeeded. Validate against database // $password is the posted password $email = $this->input->post('email'); // query the database, passing it the email & password // return an object $result = $this->login_model->login($email, $password); print_r($result); // get the user id //$id = $result[0]->id; // if a result was returned // trap the id in a session // else show an error message if($result){ $this->session->set_userdata('user_id', $id); }else{ $this->form_validation->set_message('check_database', 'Invalid email or password'); } } } 

При использовании password_hash () вам не нужно использовать MD5 с забавным, вы используете его для создания хэша пароля. С password_hash () создается более безопасный пароль без MD5.

 $hash_input_password = $this->input->post('password'); password_hash($hash_input_password, PASSWORD_BCRYPT); 

Как проверить пароль

 <?php // See the password_hash() example to see where this came from. // Some thing like $2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq' $hash = $password_from_db; if (password_verify($this->input->post('password'), $hash)) { echo 'Password is valid!'; } else { echo 'Invalid password.'; } ?> 

Будет более безопасным MD5

http://php.net/manual/en/function.password-hash.php

http://php.net/manual/en/function.password-verify.php

Пароль базы данных базы данных varchar (255)

PHP 5.5.0 +

Хеширование пароля при создании нового пользователя. Просмотреть функцию addUser внизу

 $hash_input_password = $this->input->post('password'); $password_to_db = password_hash($hash_input_password, PASSWORD_BCRYPT); 

Вход в систему Пример модели

 public function login() { $username = $this->input->post('username'); $password = $this->input->post('password'); $hashed_password = $this->confirm_password(); $this->db->where('username', $username); $this->db->where('password', password_verify($password, $hashed_password)); $user_query = $this->db->get($this->db->dbprefix . 'user'); if ($user_query->num_rows() > 0) { $set_userdata = array( 'user_id' => $user_query->row('user_id'), 'username' => $user_query->row('username') ); $this->session->set_userdata($set_userdata); return true; } else { return false; } } 

Подтвердите Пароль

 public function confirm_password() { $this->db->where('username', $this->input->post('username')); $query = $this->db->get($this->db->dbprefix . 'user'); if ($query->num_rows() > 0) { $row = $query->row('password'); $password = $row->password; } else { return false; } return $password; } 

Полный входной контроллер

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('form_validation'); } public function index() { $this->form_validation->set_rules('username', 'Username', 'required|callback_validate'); $this->form_validation->set_rules('password', 'Password', 'required'); if ($this->form_validation->run() == FALSE) { $this->load->view('login_view'); } else { redirect('dashboard'); } } public function validate() { $username = $this->input->post('username'); $password = $this->input->post('password'); if (!isset($username) || !isset($password) || !$this->login($this->input->post('username'), $this->input->post('password'))) { $this->form_validation->set_message('validate', 'No match for Username and/or Password.'); return FALSE; } } public function login($username = 0, $password = 0) { $username = $this->input->post('username'); $password = $this->input->post('password'); $hashed_password = $this->confirm_password(); $this->db->where('username', $username); $this->db->where('password', password_verify($password, $hashed_password)); $user_query = $this->db->get($this->db->dbprefix . 'user'); if ($user_query->num_rows() > 0) { $set_userdata = array( 'user_id' => $user_query->row('user_id'), 'username' => $user_query->row('username') ); $this->session->set_userdata($set_userdata); return true; } else { return false; } } public function confirm_password() { $this->db->where('username', $this->input->post('username')); $query = $this->db->get($this->db->dbprefix . 'user'); if ($query->num_rows() > 0) { $row = $query->row('password'); $password = $row->password; } else { return false; } return $password; } } 

Добавить пользователя

 public function addUser() { $hash_input_password = $this->input->post('password'); $password_to_db = password_hash($hash_input_password, PASSWORD_BCRYPT); $data = array( 'username' => $this->input->post('username'), 'password' => $password_to_db, 'firstname' => $this->input->post('firstname'), 'lastname' => $this->input->post('lastname'), 'email' => $this->input->post('email'), 'status' => "1", 'date_added' => mdate('%Y-%m-%d %H:%i:%s', now()) // Need to load date helper ); $this->db->insert($this->db->dbprefix . 'user', $data); }