[eluser]Unknown[/eluser]
So I'm trying to restrict a page to only be viewed if the user is logged in. I put the session verification in the construct and what ends up happening is both my access denied page and the page that should only be viewable if logged in both get loaded. Here's my two controllers if anybody has any ideas.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function index() {
$data['main_content'] = 'login_page';
$this->load->view('includes/template',$data);
}
public function validate_user() {
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if ($query) {
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => TRUE
);
$this->session->set_userdata($data);
redirect('site');
} else {
$this->index();
}
}
}
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function __construct(){
parent::__construct();
$this->logged_in();
}
public function logged_in(){
$logged_in = $this->session->userdata('is_logged_in');
if(!isset($logged_in) || $logged_in != true) {
$data['main_content'] = 'denied_page';
$this->load->view('includes/template', $data);
}
}
public function index() {
$data['main_content'] = 'files_page';
$this->load->view('includes/template', $data);
}
}
I tried renaming the index() function in the Site controller to something else in case that was the cause of the problem and it still did the same thing. Here's the page source from my browser if I try to go directly to /site/ without logging in
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="http://localhost/ci_project/css/style.css" type="text/css">
</head>
<body>
<p>You do not have permission to view this page. <a href="http://localhost/ci_project/login">Click here to login.</a></p>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="http://localhost/ci_project/css/style.css" type="text/css">
</head>
<body>
made it
</body>
</html>