当我输入数据时,我的登录不起作用,它将始终恢复为登录视图...我创建它,所以如果我输入数据,它会转到前面/主页,但不知何故,if in 函数登录不起作用,并且总是将其更改为其他...
控制器:登录.php
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library(array('session', 'form_validation'));
$this->load->helper(array('url', 'form'));
$this->load->model("Member_model");
}
public function loginMember() {
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('front/login');
} else {
//enter username and password
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('Member_model');
$member = $this->Member_model->checkLogin($username, $password);
if ($member) {
$id_member = $this->Member_model->get_member_id_from_username($username);
$member = $this->Member_model->get_member($id_member);
//set session variables
$_SESSION['id_member'] = $member->id;
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $member->username;
//redirect
redirect('Member/profile');
} else {
print_r('User doesnt exist');
}
}
}
//logging out of a user
public function logoutMember() {
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
foreach ($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
redirect('Member/Login');
}
}
}
控制者:成员.php
public function profile() {
if (isset($_SESSION['user_logged']) && $_SESSION['user_logged'] !== true) {
$this->session->set_flashdata("error","Please login first to view");
redirect('Member/Login');
} else{
$this->load->view('front/home');
}
}
型号:Member_model.php
public function checkLogin($username, $password) {
$this->db->select('password');
$this->db->from('member');
$this->db->where('username', $username);
$hash = $this->db->get()->row('password');
return $this->verify_password_hash($password, $hash);
}
public function get_member_id_from_username($username) {
$this->db->select('id');
$this->db->from('member');
$this->db->where('username', $username);
return $this->db->get()->row('id');
}
public function get_member($id_member) {
$this->db->from('member');
$this->db->where('id', $id_member);
return $this->db->get()->row();
}
private function hash_password($password) {
return password_hash($password, PASSWORD_BCRYPT);
}
private function verify_password_hash($password, $hash) {
return password_verify($password, $hash);
}
查看:登录.php
<form method="post" action="<?php echo site_url('Login/loginMember'); ?>" >
<input class="text" type="text" id="username" name="username" placeholder="Your username"><br>
<input class="text" type="password" id="password" name="password" placeholder="Your password">
<input type="submit" value="Login">
</form>
<p>Don't have an Account? <a href="<?php echo site_url('Member/Register'); ?>"> SignUp NOW!</a></p>
我认为问题出在这段代码中:
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('front/login');
} else {
//enter username and password
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('Member_model');
$member = $this->Member_model->checkLogin($username, $password);
if ($member) {
$id_member = $this->Member_model->get_member_id_from_username($username);
$member = $this->Member_model->get_member($id_member);
//set session variables
$_SESSION['id_member'] = $member->id;
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $member->username;
//redirect
redirect('Member/profile');
} else {
print_r('User doesnt exist');
}
}
因为,在我输入数据后,它会打印出用户不存在......即使我输入的数据看起来就像数据库中的数据。
在成员控制器中更改 if 条件。您忘记了其中的"="。
if ($_SESSION['user_logged'] == FALSE)
并检查您的会话变量是否存储数据。