2013-03-05 50 views
-1

我有一個會話,我正在使用數據庫進行會話。我在會話中使用userdata來存儲用戶的名稱和「登錄」標誌。我是自動加載會話庫,所以我不必顯式加載它。預期的CodeIgniter會話行爲?

我在控制器,檢查「已登錄」和視圖中使用它來顯示用戶的名稱,這導致CI_Session被加載兩次並導致會話被銷燬。兩次CI_Session加載它試圖更新會話和數據庫,第一個成功,第二個失敗並銷燬會話。

我認爲我違反了控制器和視圖之間的分離,我只用在控制器中的會話庫,並通過與用戶名變量的視圖解決了這個問題。

但我的問題是:分析我做了正確的嗎?我是否違反了控制器和視圖之間的分離,只要我不再這樣做,我應該沒問題,或者這可能會在其他情況下再次出現?

注:我想仔細問這個問題,我在尋找技術解答不是的意見,我不希望這成爲本VS,一個討論等

添加代碼要求:

段從控制器admin.php的

class Admin extends CI_Controller { 
    function __construct() { 
    parent::__construct(); 
    $this->load->model('users'); 
    $this->id = $this->session->userdata('id'); 
    $this->authorized = array('waccess' => $this->users->authorizedUser($this->id, 'waccess'), 
           'ceditor' => $this->users->authorizedUser($this->id, 'ceditor'), 
           'uadmin' => $this->users->authorizedUser($this->id, 'uadmin'), 
           'forms' => $this->users->authorizedUser($this->id, 'forms')); 

    } 
    public function index() { 
    log_message('debug', 'Admin->index'); 
    $this->load->view('framework', array(
     'head' => $this->load->view('head', array('title' => 'Administrator', 'stylesheet' => 'admin.css', 'javascript' => 'jquery-ui-1.8.16.custom.min.js'), true), 
     'header' => $this->load->view('headerAdmin', array('active' => 'Home', 'authorized' => $this->authorized), true), 
     'body' => $this->load->view('adminHome', '', true), 
     'midBody' => $this->load->view('blankMid', '', true), 
     'footer' => $this->load->view('footer', '', true) 
    )); 
    } 

片段從View adminHome.php

<div id="main"> 
    <div class="content"> 
    <h1>Employee Interface</h1> 
<? if(!$this->session->userdata('loggedin')): ?> 
.... 
<? else: ?> 
    <p>Welcome <?=$this->session->userdata('fname')?> <?=$this->session->userdata('lname')?></p> 
<? endif; ?> 
    <p>Use the menu above to select the various employee and administrative options available to you</p> 
    </div> 
</div> 

我加了很多調試代碼,但沒有其他變化的CodeIgniter代碼,看看發生了什麼事情,這裏生成的日誌條目:

DEBUG - 2013-03-04 19:54:31 --> Config Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Hooks Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Utf8 Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> UTF-8 Support Enabled 
DEBUG - 2013-03-04 19:54:31 --> URI Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Router Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Output Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Security Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Input Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Global POST and COOKIE data sanitized 
DEBUG - 2013-03-04 19:54:31 --> Language Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Loader Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Helper loaded: url_helper 
DEBUG - 2013-03-04 19:54:31 --> loading: session 
DEBUG - 2013-03-04 19:54:31 --> Session Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Helper loaded: string_helper 
DEBUG - 2013-03-04 19:54:31 --> Database Driver Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Session using database 
DEBUG - 2013-03-04 19:54:31 --> Session matching on [session_id]: b791b771c776ca4166a73424315d1110 
DEBUG - 2013-03-04 19:54:31 --> Session matching on [user_agent]: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 
DEBUG - 2013-03-04 19:54:31 --> Session Updating 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [session_id] => b791b771c776ca4166a73424315d1110 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [ip_address] => 184.4.66.94 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [last_activity] => 1362444838 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [user_data] => 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [id] => 15 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [fname] => Test 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [lname] => Test 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [email] => t 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [loggedin] => 1 
DEBUG - 2013-03-04 19:54:31 --> Session Update Completed 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [session_id] => 7875df72dc94ca7bd149debe69865a2e 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [ip_address] => 184.4.66.94 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [last_activity] => 1362444871 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [user_data] => 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [id] => 15 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [fname] => Test 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [lname] => Test 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [email] => t 
DEBUG - 2013-03-04 19:54:31 --> Session Data: [loggedin] => 1 
DEBUG - 2013-03-04 19:54:31 --> Session routines successfully run 
DEBUG - 2013-03-04 19:54:31 --> Controller Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Model Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> loading: session 
DEBUG - 2013-03-04 19:54:31 --> Session Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Database Driver Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Session using database 
DEBUG - 2013-03-04 19:54:31 --> Session matching on [session_id]: b791b771c776ca4166a73424315d1110 
DEBUG - 2013-03-04 19:54:31 --> Session matching on [user_agent]: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 
DEBUG - 2013-03-04 19:54:31 --> Session not found, destroying instance 
DEBUG - 2013-03-04 19:54:31 --> Session routines successfully run 
DEBUG - 2013-03-04 19:54:31 --> Controller Class Initialized 
DEBUG - 2013-03-04 19:54:31 --> Admin->index 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/head.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/headerAdmin.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/adminHome.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/blankMid.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/footer.php 
DEBUG - 2013-03-04 19:54:31 --> File loaded: application/views/framework.php 
DEBUG - 2013-03-04 19:54:31 --> Final output sent to browser 
DEBUG - 2013-03-04 19:54:31 --> Total execution time: 0.0793 
+1

無似乎還有其他錯誤。你能告訴我們一些代碼嗎?你如何使用會話方法?應該同時使用的控制器和視圖會議通話沒問題.. – jtheman 2013-03-05 00:38:05

+0

我想補充一點,大的原因,我要問這個問題,一個是因爲我找不到任何答案的問題的原因,當我首先開始使用它,並且我花了很多時間去深入瞭解Codeigniter代碼,以弄清楚我做錯了什麼。 – TheDavidFactor 2013-03-05 00:41:05

+0

@jtheman當然,給我幾分鐘 – TheDavidFactor 2013-03-05 00:42:41

回答

0

嘗試更改配置設置:

$config['sess_match_useragent'] = FALSE; 

我的猜測是,你有某種無論是代碼的在您的視圖或其他腳本或瀏覽器插件,攪亂用戶代理檢查。這會導致會話匹配不匹配,導致您看到結果Session not found, destroying instance,導致您的錯誤。

當然,這一變化的結果是不太安全的會議,因此,如果安全是一個大問題,你可以深入瞭解一下是什麼導致課程的問題。

您可以找到其他職位信息...

Googlechrome框架是原因(由引導模板的默認部分): Codeigniter sessions being destroyed in IE 10 when changing pages

FirePHP擴展的原因: http://blog.tiger-workshop.com/firephp-firefox-extension-causing-codeigniter-session-lost/

+0

它似乎沒有用useragent的問題,它實際上似乎與ajax問題更密切相關,但沒有cookie問題:[found here](http://stackoverflow.com/questions/7980193/ codeigniter-session-bugging-out-with-ajax-calls) – TheDavidFactor 2013-03-06 18:30:07

+0

那麼你向我們展示或告訴我們任何內部的AJAX調用。然後這個問題不像我們初次看到的那樣是線性的......尋求幫助的好開始是添加與問題相關的所有事情。 – jtheman 2013-03-06 20:05:59