My login and logout session is not function right in if, I don't know what to do it always caught on else than in if code. Even though the username and password that i input is the same as the one on the database
Controller: Login.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');
}
}
Model: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);
}
View:Home.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>
I don't know what to do, the if($member) on the loginMember code in Login Controller is not working and always get to else every time I input data. But when I comment the if($member) like this:
$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');
//
// }
It's working perfectly, but I cannot use the session on view home when I called session username to show who is login that time. And I called the view in Controller Member, like this:
Controller:Member.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');
}
try in model
if ($hash->num_rows() > 0) {
return $hash->row();
} else {
return FALSE;
}
controller:
if (!$member)
Check in your index method of your controller:
public function index(){
if( $this->session->userdata('logged_in'))
return redirect('Dashboard');
$this->load->view('login');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.