0

私のプロジェクトは、教師と生徒の 2 種類のユーザーを持つ学生教師コミュニティ Web サイトです。次に、別のコントローラーにアクセスした後、最初にホームページにアクセスし、ユーザーがホームコントローラーからログアウトしてブラウザーの戻るボタンを押すと、ログインページが表示されますが、ユーザーが2回目に戻るボタンを押すと、前のセッションが表示されます。そのため、セッションは他のコントローラーでは機能しません。コントローラーとビューを以下に示します。戻るボタンの問題を防ぐにはどうすればよいですか? ログイン ビュー:

<!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>

verify login コントローラーの下: 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;
   }
 }
}
?>

次に、以下のホームコントローラーに移動します。

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

}

?>

次に、以下のAnswer コントローラーのような別のコントローラーを入力します。

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コントローラーからログアウトした後、最初に戻るボタンをクリックしてセッションが機能し、ログインページにリダイレクトされますが、別のコントローラーセッションに移動するときに戻るボタンを2回押すと機能しません。誰か助けてください......

4

0 に答える 0