2012-01-14 154 views
2

我已經在我的codeigniter應用程序中使用真棒codeigniter authentication library ion auth創建了用戶身份驗證,身份驗證可以正常工作,但是當我註銷並單擊瀏覽器的按鈕時,我可以瀏覽我訪問過的所有頁面引起用戶隱私的關注的應用程序,但如果我嘗試刷新頁面,則認識到我已註銷。當用戶點擊瀏覽器的返回按鈕時,如何強制瀏覽器重新加載?如何解決這個問題的任何建議將感激..Codeigniter會話問題

編輯

控制器註銷功能

function logout() { 
    //log the user out 
    $logout = $this->ion_auth->logout(); 

    //redirect them back to the page they came from 
    redirect('auth', 'refresh'); 
} 

這是從ion auth

public function logout() { 
    $this->ci->ion_auth_model->trigger_events('logout'); 

    $identity = $this->ci->config->item('identity', 'ion_auth'); 
    $this->ci->session->unset_userdata($identity); 
    $this->ci->session->unset_userdata('group'); 
    $this->ci->session->unset_userdata('id'); 
    $this->ci->session->unset_userdata('user_id'); 

    //delete the remember me cookies if they exist 
    if (get_cookie('identity')) { 
     delete_cookie('identity'); 
    } 
    if (get_cookie('remember_code')) { 
     delete_cookie('remember_code'); 
    } 

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

    $this->set_message('logout_successful'); 
    return TRUE; 
} 

我註銷功能使用codeigniter 2.0.3

Thanx提前..

+0

顯示一些代碼...我敢打賭,你不檢查「被記錄在」這就是probly每一頁上爲什麼,只是做在登錄時。需要檢查您的'安全區域'的每頁 – Jakub 2012-01-14 08:14:40

+0

我確實檢查了這就是爲什麼當我重新加載頁面時,它會將我重定向到登錄頁面,它看起來像某人註銷時頁面不會重新加載 – 2012-01-14 08:20:33

+0

也許您不完全註銷,很難說沒有任何代碼,發佈一個典型的控制器設置,因爲我從來沒有在CI上的會話中遇到過這個問題。 – Jakub 2012-01-14 15:58:32

回答

6

機會是他們實際上註銷(如你所說,刷新導致他們出現登出)。瀏覽器很可能已經緩存了顯示的HTML,表明它們已經登錄,但在註銷後不會重新加載它。

通過設置Cache-Control標題,您可以將與登錄相關的信息設置爲無緩存的頁面。

這可以用HTML

<META Http-Equiv="Cache-Control" Content="no-cache"> 
<META Http-Equiv="Pragma" Content="no-cache"> 
<META Http-Equiv="Expires" Content="0"> 

或PHP

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past 

實現您還可以實現使用下面的代碼,特別是窗口中的哈克並不可取清除用戶的歷史。這需要作爲註銷功能的一部分發送到瀏覽器,並且如果用戶禁用了javascript,則不起作用。

<script language="javascript"> 
    var Backlen=history.length; 
    history.go(-Backlen); 
    window.location.href=page url 
</SCRIPT> 
+0

Thanx很多@Ben Swinburne ..這是緩存問題。我在codeigniter中通過添加'$ this-> output-> set_header(「Cache-Control:no-store,no-cache,must-revalidate,no-transform,max-age = 0,post-check = 0,預檢查= 0" ); $ this-> output-> set_header(「Pragma:no-cache」);'在除認證控制器之外的每個控制器的構造函數處。 – 2012-01-15 09:48:30

+0

嘗試在控制器級別的數據庫交互功能上重定向,而不是禁用緩存。 – serdarsenay 2013-02-20 01:49:00

+0

@SamSamson:我不明白爲什麼你不會在任何地方緩存除了「身份驗證控制器」。我認爲每個人都使用會話的ion_auth。所以no-cache在「認證控制器」中也應該沒問題。 – neelabh 2015-07-07 15:11:04

1

我非常不鼓勵禁用緩存,因爲這會降低您的應用程序的速度。我曾因爲傻編碼的同樣的問題,我所做的就是在我的控制器,我把下面的代碼在其與數據庫交互的每個功能,用戶所使用的頂部登錄狀態:

//Do not let anyone interact with database from cached pages! 
    if (!$this->tank_auth->is_logged_in()) { 
     redirect('/auth/login/'); 
    } 

因此,重定向到登錄頁面會發生,也就是刷新,如果註銷用戶的瀏覽器被「支持」到緩存登錄狀態並試圖擺弄數據庫。

0

是離子認證有這個問題我在我的應用程序面臨同樣的問題。 anothr問題是,如果會話在任何鏈接上過期,它將使您登錄頁面。但是當您登錄並嘗試訪問會話過期的最後一個鏈接時,它總會將您帶回登錄頁面。要訪問您需要清除瀏覽器緩存的頁面。這裏是解決方案,我在github評論發現離子權威性

github ion-auth comment link

function logout() { 
    //log the user out 
    $logout = $this->ion_auth->logout(); 

    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
    header("Expires: Wed, 4 Jul 2012 05:00:00 GMT"); // Date in the past 

    //redirect them back to the page they came from 
    redirect('auth', 'refresh'); 
}