My project is student teacher community website it has two type user teacher and student. then first they visit home page after they visit another controller then when user press logout from home controller and press back button in browser then the login page come but when user press back button second time its shows the previous session . so session not work for the other controller . I show my controllers and views below . how can i prevent back button problem ? login view:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php include_once('loginhead.php'); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple Login with CodeIgniter</title>
</head>
<body onunload="javascript:history.go(1)">
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<div class="maincontent_area">
<div class="container">
<div class="row">
<div class="span7">
<div class="carousel slide" id="artists">
<ol class="carousel-indicators">
</ol>
<div class="carousel-inner">
<div class="item active">
<a href="#"><img src="<?php echo base_url().'img/image_1.jpg'; ?>" alt="" /></a>
<div class="carousel-caption">
<h4>Online Educational Community Forum</h4>
</div>
</div>
<div class="item">
<a href="#"><img src="<?php echo base_url().'img/image_2.jpg'; ?>" alt="" /></a>
<div class="carousel-caption">
<h4>Online Educational Community Forum</h4>
</div>
</div>
<div class="item">
<a href="#"><img src="<?php echo base_url().'img/image_3.jpg'; ?>" alt="" /></a>
<div class="carousel-caption">
<h4>Online Educational Community Forum</h4>
</div>
</div>
<div class="item">
<a href="#"><img src="<?php echo base_url().'img/image_4.jpg'; ?>" alt="" /></a>
<div class="carousel-caption">
<h4>Online Educational Community Forum</h4>
</div>
</div>
</div>
<a class="carousel-control left" href="#artists" data-slide="prev"><</a>
<a class="carousel-control right" href="#artists" data-slide="next">></a>
</div>
</div>
<div class="span5">
<div class="login_area">
<h4 class="text-info">Login</h4>
<p class="text-success">Members only</p>
<form action="http://localhost/Login/index.php/verifylogin" method="post" accept-charset="utf-8"> <label for="username">Username:</label>
<input type="text" size="20" id="username" name="username" required/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password" required/>
<br/>
<input class="btn btn-info" type="submit" value="LogIn"/><br>
<h6><a href="http://localhost/Login/index.php/Forgotpass/forgot_password">Forgot password</a></h6>
</form>
</div>
<div class="signup_area">
<h4 class="text-info">Sign Up</h4>
<p class="text-success">If you are not a Member yet.</p>
<script>
$(document).ready(function() {
$("#div1").hide();
$("#div2").hide();
$("#student").hide();
$("#teacher").hide();
$("#clear").hide();
$("#signup").click(function() {
$("#student").show();
$("#teacher").show();
//$("#div1").show();
$("#signup").hide();
});
$("#student").click(function(){
$("#student").hide();
$("#div1").show();
$("#div2").hide();
$("#clear").show();
})
$("#teacher").click(function(){
$("#div1").hide();
$("#div2").show();
$("#teacher").hide();
$("#clear").show();
$("#signup").hide();
})
$("#clear").click(function(){
$("#div1").hide();
$("#div2").hide();
$("#teacher").hide();
$("#clear").hide();
$("#student").hide();
$("#signup").show();
})
});
</script>
<p id="signup" class="btn btn-primary">Click Here</p>
<p id="student" class="btn btn-info">Student</p>
<p id="teacher" class="btn btn-info">Teacher</p>
<p id="clear" class="btn btn-inverse">Clear</p>
<div id="div1">
<h3 class="text-info">For Student SignUp</h3>
<form action="http://localhost/Login/index.php/student_signup/registration" method="post" accept-charset="utf-8"> <label for="name">Name:</label>
<input type="text" name="sign_name" id="name" required/>
<input type="hidden" value="0" name="flag"/>
<br/>
<label for="email">ID:</label>
<input type="text" name="sign_id" id="id" required/>
<br/>
<label for="email">Email:</label>
<input type="text" name="sign_email" id="email" required/>
<br/>
<label for="password">Password: </label>
<input type="password" name="sign_password" id="sign_password" required/>
<br/>
<label for="confirm_password">Confirm Password</label>
<input type="password" name="sign_confirm_password" id="sign_confirm_password" required/>
<br/>
<input class="btn btn-primary" type="submit" value="Submit"/>
</form>
</div>
<div id="div2">
<h3 class="text-info">Teacher's SignUp</h3>
<form action="http://localhost/Login/index.php/teacher_signup/t_registration" method="post" accept-charset="utf-8"> <label for="name">Teacher Name:</label>
<input type="text" name="teacher_sign_name" id="name" required/>
<input type="hidden" value="1" name="flag"/>
<br/>
<label for="email">ID:</label>
<input type="text" name="teacher_sign_id" id="id" required/>
<br/>
<label for="email">Email:</label>
<input type="text" name="teacher_sign_email" id="email" required/>
<br/>
<label for="password">Password: </label>
<input type="password" name="teacher_sign_password" id="sign_password" required/>
<br/>
<label for="confirm_password">Confirm Password</label>
<input type="password" name="teacher_sign_confirm_password" id="confirm_password" required/>
<br/>
<input class="btn btn-primary" type="submit" value="Submit"/>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
below the verify login controller: login check
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == TRUE)
{
//Field validation failed. User redirected to login page
$username_flag = $this->input->post('username');
$password_flag= $this->input->post('password');
$confirm_page = $this->user->just_flag($username_flag, $password_flag);
if($confirm_page)
{
$sess_array = array();
foreach($confirm_page as $row)
{
$sess_array = array(
//'id' => $row->id,
'flag' => $row->flag
);
}
}
if($sess_array['flag'] == 0){
redirect('home', 'refresh');
}
else
{
redirect('home1', 'refresh');
}
}
else
{
//Go to private area
$this->load->view('login_view');
}
}
function check_database()
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
$password = $this->input->post('password');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username,
'flag' => $row->flag,
'logged_in'=> true
);
$this->session->set_userdata($sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
Then it goes to Home controller below :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Home1 extends CI_Controller {
function __construct()
{
parent::__construct();
// $this->output->nocache();
header("Expires: Tue, 01 Jan 2013 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$this->load->model('user','',TRUE);
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('teacherview', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout1()
{
$this->session->unset_userdata('logged_in');
$this->session->sess_destroy();
redirect('home1', 'refresh');
}
}
?>
then when enter another controller like below Answer controller:
class AnswerController extends CI_Controller {
public function index() {
$loggedIn = $this->session->userdata('logged_in');
if ($loggedIn) {
$config['base_url'] = base_url() . 'index.php/AnswerController/index';
$config['total_rows'] = $this->db->count_all('questions');
$config['per_page'] = 9;
$this->load->library('pagination');
$this->pagination->initialize($config);
//$this->load->model('Answer');
$this->load->model('Answer');
$data['questions'] = $this->Answer->getQuestions($config['per_page'], $this->uri->segment(3));
$this->load->view('answerView', $data);
} else {
redirect('login', 'refresh');
}
}
then click the anchor like below logout:
anchor('home1/logout1','Logout');
after logout from home1 controller then frist back button click session work and redirect to login page but when press back button second time when it goes to another controller session not work please anybody help me......