i have 2 controllers (AuthController.php, AdminController.php) and one master page for a login/registration system. the sess_destroy() is in AuthController and i used construct method to check if the user is logged in or not, if logged in it redirect to (AdminController/home). i put an anchor in the master page to logout(or destroy session). the problem is when i click on logout the session is not destroying. the page is redirects to (Authcontroller/login) and again redirecting to (AdminController/home).
AuthController.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class AuthController extends CI_Controller{
public function __construct()
{
parent::__construct();
if($this->session->userdata('is_logged_in')){
redirect('AdminController/home', 'refresh');
}
}
public function index()
{
$this->login();
}
public function login()
{
$data = [
'title'=>'Welcome to Admin Panel',
'include'=>'admin/login'
];
$this->load->view('admin/master', $data);
}
public function login_validation()
{
$data = [
'title' => 'Error Login'
];
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean|callback_validate_credential');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
if($this->form_validation->run()){
$data = [
'username' => $this->input->post('username'),
'is_logged_in' => 1
];
$this->session->set_userdata($data);
redirect('AdminController/home');
}else{
$this->load->view('admin/login', $data);
}
}
public function register()
{
$data = [
'title' => 'Create New Account',
'include' => 'admin/register'
];
$this->load->view('admin/master', $data);
}
public function registration_validation()
{
$data = [
'title' => 'Error Creating Account',
];
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Full Name', 'required|trim');
$this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[users.username]');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|trim|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]');
$this->form_validation->set_message('is_unique', 'The username or password already exist!');
if($this->form_validation->run()){
$this->load->model('users','',true);
$this->users->addUser();
redirect('AuthController/index');
}else{
$this->load->view('admin/register', $data);
}
}
public function validate_credential()
{
$this->load->model('users');
if($this->users->can_log_in()){
return true;
}else{
$this->form_validation->set_message('validate_credential', 'The username or password not exist');
return false;
}
}
public function logout()
{
$this->session->sess_destroy();
redirect('AuthController/login', 'refresh');
}
}
AdminController.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class AdminController extends CI_Controller{
public function index()
{
$this->home();
}
public function home()
{
$data = [
'title' => 'Welcome to Admin Panel',
'include' => 'admin/home'
];
$this->load->view('admin/master', $data);
var_dump($this->session->userdata());
}
}
master.php:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= $title ?></title>
<link rel="stylesheet" href="<?=base_url()?>/assets/css/bootstrap.min.css"/>
<link rel="stylesheet" href="<?=base_url()?>/assets/css/admin.css"/>
<script src="<?=base_url()?>/assets/js/jquery.min.js"></script>
<script src="<?=base_url()?>/assets/js/bootstrap.min.js"></script>
<script src="<?=base_url()?>/assets/js/respond.js"></script>
</head>
<body>
<div class="container">
<?php
echo anchor('AuthController/logout', 'LOGOUT');
$this->load->view($include)
?>
</div>
</body>