0

我的项目是学生教师社区网站,它有两种类型的用户教师和学生。然后首先他们在访问另一个控制器后访问主页,然后当用户从家庭控制器按注销并在浏览器中按后退按钮时,登录页面就会出现,但是当用户第二次按后退按钮时,它会显示上一个会话。所以会话不适用于其他控制器。我在下面展示我的控制器和视图。如何防止后退按钮问题? 登录视图:

<!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">&#60;</a>
                    <a class="carousel-control right" href="#artists" data-slide="next">&#62;</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>

在验证登录控制器下方:登录检查

<?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;
   }
 }
}
?>

然后它转到下面的 Home 控制器:

<?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');
 }

}

?>

然后当输入另一个控制器时,如下面的答案控制器:

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');
    }
}

然后单击下面的锚点注销:

anchor('home1/logout1','Logout');

从home1控制器注销后,第一个返回按钮单击会话工作并重定向到登录页面但是当第二次按下返回按钮时,当它进入另一个控制器会话时不起作用请任何人帮助我......

4

0 回答 0