无法使用php登录
问题描述:
我正在使用PHP CodeIgniter框架。 这里是login.php控制器。无法使用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'
文件名:控制器/ login.php中
行号:38
以及其他字符串session_array
答
删除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');
}
}
根据错误消息,你的'$ row'变量是一个字符串。 – CBroe