我正在使用PHP CodeIgniter框架。 这是login.php控制器。
public function index()
{
if($this->input->post('login') == 1)
{
$user = new Users_model();
$user->email = $this->input->post('email');
$user->password = $this->input->post('password');
$user->user_role = $this->input->post('user_role');
$results = $this->usermodel->login($user);
if(count($results)>0)
{
if(is_array($results) || is_object($results))
{
foreach($results as $row)
{
$session_array = array(
"id" => $row['id'],
"username" => $row['username'],
"email" => $row['email'],
"password" => $row['password'],
"image" => $row['image'],
"description" => $row['description'],
"account_status" => $row['account_status'],
"user_role" => $row['user_role']
);
$this->session->set_userdata($session_array);
$url = base_url() . "home?login=success";
redirect($url, "refresh");
}
}
}else{
$url = base_url() . "login?login=failed";
redirect($url, "refresh");
}
}else{
$this->load->view('login-page');
}
}
这是模型中的Users_model.php。
function login($user){
$conditions = array(
"email" => $user->email,
// "password" => $user->password,
"user_role" => $user->user_role,
"account_status" => 1,
);
$this->db->select('*');
$this->db->from('users');
$this->db->where($conditions);
$rs= $this->db->get();
if(!empty($rs)){
$result_array = $rs->row_array();
if(password_verify($user->password,$result_array['password'])){
return $result_array;
}
}
return false;
}
password_hash和password_verify是否正确? 以下是错误代码:
严重性:警告
消息:非法字符串偏移'id'
文件名:controllers / Login.php
行号:38
以及session_array中的其他字符串
答案 0 :(得分:0)
删除foreach循环,一切都很好
public function index()
{
if($this->input->post('login') == 1)
{
$user = new Users_model();
$user->email = $this->input->post('email');
$user->password = $this->input->post('password');
$user->user_role = $this->input->post('user_role');
$results = $this->usermodel->login($user);
if(count($results)>0)
{
if(is_array($results) || is_object($results))
{
$session_array = array(
"id" => $results['id'],
"username" => $results['username'],
"email" => $results['email'],
"password" => $results['password'],
"image" => $results['image'],
"description" => $results['description'],
"account_status" => $results['account_status'],
"user_role" => $results['user_role']
);
$this->session->set_userdata($session_array);
$url = base_url() . "home?login=success";
redirect($url, "refresh");
}
}
else
{
$url = base_url() . "login?login=failed";
redirect($url, "refresh");
}
}
else
{
$this->load->view('login-page');
}
}