我的登录和注销会话在if中不起作用,我不知道该怎么做,它总是在if代码中捕获。即使我输入的用户名和密码与数据库上的用户名和密码相同
控制器:登录.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 > 0) {
$data_session = array(
'nama' => $member->username,
'logged_in' => true
);
$this->session->set_userdata($data_session);
//redirect
redirect('Member/profile');
} else {
print_r('User doesnt exist');
}
}
}
//logging out of a user
public function logoutMember() {
$this->session->sess_destroy();
redirect('Member/Login');
}
}
型号:Member_model.php
public function checkLogin($username, $password) {
$this->db->select('password');
$this->db->from('member');
$this->db->where(array('username' => $username, 'password'=> $password));
$hash = $this->db->get()->row('password');
return $this->verify_password_hash($password, $hash);
if ($hash->num_rows()>0) {
return $hash->row();
} else {
return false;
}
}
private function hash_password() {
return password_hash($password, PASSWORD_BCRYPT);
}
private function verify_password_hash($password, $hash) {
return password_verify($password, $hash);
}
查看:首页.php
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<?php echo $this->session->userdata("nama");?>
Member<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="<?php echo site_url('checkout')?>"><img src="<?php echo base_url('assets/img/shopping-cart.png')?>" style="height:15px;width:15px;"> Checkout</a></li>
<li role="separator" class="divider"></li>
<li><a href="<?php echo base_url('login/logoutMember')?>">Logout</a></li>
</ul>
</li>
我不知道该怎么办,登录控制器中登录成员代码上的 if($member( 不起作用,每次输入数据时总是会转到其他位置。但是当我这样评论if($member(时:
$this->load->model('Member_model');
$member = $this->Member_model->checkLogin($username, $password);
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $member->username;
redirect('Member/profile');
// if ($member > 0) {
//
// $data_session = array(
// 'nama' => $member->username,
// 'logged_in' => true
// );
// $this->session->set_userdata($data_session);
//
// //set session variables
//// $_SESSION['logged_in'] = true;
//// $_SESSION['username'] = $member->username;
//
// //redirect
// redirect('Member/profile');
//
// } else {
//
// print_r('User doesnt exist');
//
// }
它运行良好,但是当我调用会话用户名以显示当时谁登录时,我无法使用View home上的会话。 我在控制器成员中调用了视图,如下所示:
控制者:成员.php
public function profile() {
if( $this->session->userdata('logged_in')){
$data["slider"] = $this->Indexweb_model->getAll();
return $this->load->view('front/home', $data);
}
redirect('Member/Login');
}
在模型中尝试
if ($hash->num_rows() > 0) {
return $hash->row();
} else {
return FALSE;
}
控制器:
if (!$member)
签入控制器的索引方法:
public function index(){
if( $this->session->userdata('logged_in'))
return redirect('Dashboard');
$this->load->view('login');
}