2016-09-28 113 views
0

創建了一個簡單的會話頁面,即使從頁面註銷後,我仍然可以訪問登錄頁面。 我也銷燬了所有會話,但仍找不到任何解決方案。codeigniter - 註銷後仍然可以使用的後退按鈕

視圖 - flashdata_home.php

<form action='add' method='post'> 

    <input type ='text' name='value'/> 
    <input type='submit' value='Enter ' /> 

</form> 

控制器 - FlashData_Controller.php

<?php 

class FlashData_Controller Extends CI_Controller { 


    function __construct() { 
     parent::__construct(); 

     $this->load->library('session'); 
     $this->load->helper('url'); 

    } 

    public function index(){ 
     $this->load->view('flashdata_home'); 
    } 

    public function add(){ 
    // adding flash data 
    //$this->session->set_flashdata('item','This is me'); 

    $this->session->set_userdata('Name',$this->input->post('value')); 

    //redirect to home page 
    // redirect('flashdata'); 

    if($this->session->has_userdata('Name')){ 

     $data = array('value' => $this->session->Name); 
     $this->load->view('adminflashdata_home',$data); 
    } 
    else 
    { 
     $this->load->view('flashdata_home'); 
    } 
    } 

    public function logout(){ 

     $this->session->unset_userdata('Name'); 
     $this->session->sess_destroy('Name'); 
     $this->load->view('flashdata_home'); 

    } 

} 

視圖 - adminflashdata_home.php

<?php 
echo $value; 
<li><a href="logout">Logout</a></li> 
?> 

回答

1

令人不安的CI中的會話非常簡單,看起來像這樣。

在您的代碼中,您已取消設置數據,但您必須像我一樣取消設置變量。

對於單數據:

$this->session->unset_userdata('some_name'); 

對於數據單項的數組:

$array_items = array('username' => '', 'email' => ''); 
$this->session->unset_userdata($array_items); 

對於破壞會話:

$this->session->sess_destroy(); 
0

我認爲你的問題是,雖然我們銷燬會話,但我們仍然可以訪問只有當用戶登錄時才應該加載的頁面。

例如,當用戶使用正確的憑據登錄時,url應如下所示:本地主機/應用程序/控制器/功能(例如)。稍後當用戶註銷時,您將重定向回登錄頁面。但是如果我們在url中輸入localhost/app/controller/function,或者如果我們在瀏覽器中點擊返回按鈕,瀏覽器將加載頁面!我認爲你說的問題是這樣的。

對於這個問題,我總是在控制器的每個功能中使用一個解決方案。喜歡;

class MainController extends CI_Controller { 
    function test { 
    $user_name = $this->session->userdata('user_name'); 
    if(isset($user_name)) { 
     //the actual function code goes here 
    } 
    else { 
     //redirect to the login function 
    } 
    } 
} 

我希望這有助於有人..歡呼聲..

+0

仍然沒有工作 –

+0

你能一次請檢查會話變量是否設置正確與否?只需在登錄後回顯所有會話變量。 –

+0

是的會話變量已設置。但直到現在還找不到任何解決方案。 –