2011-12-13 71 views
0

我試圖在我的登錄表單中實現「記住我」複選框。望着文檔我看到,我可以改變默認的配置設置:根據用戶選擇更改CodeIgniter會話配置

$config['sess_expiration'] = 10; // I set it to 10 seconds for testing 

我試圖做的是無限期延長這個時間,或者至少要長得多,如果用戶檢查框。當輸入通過POST傳遞時,我正在更改sess_expiration的值,並且我可以看到該值已更改,但它仍在默認時間之後過期,而不是在新時間之後過期。

if ($this->input->post('rememberMe')) 
{ 
    $this->config->set_item('sess_expiration', 0); 
} 

就在我設置我稱這個,如果我贊同出來我看值設置正確,但我等待,然後我會走了,我重定向回到我的登錄頁面的會話信息。

$this->session->set_userdata(
    array('user' => array(
     'userId' => $user->userId, 
     'userName' => $user->userName, 
     'userLevel' => $user->userLevel, 
     'userEmail' => $user->userEmail, 
     'userCreated' => $user->userCreated, 
     'userUpdated' => $user->userUpdated, 
     'userStatus' => $user->userStatus 
    ), 
    'loggedIn' => $user->userId 
)); 

我做錯了嗎?我在這個問題上花費了太多時間,所以我會繼續前進,並希望這裏的某個人能夠指出我做得不好。

謝謝!

回答

1

你有沒有想過設定本餅乾嗎?我認爲你最好把數據存儲到用戶,所以每次他們返回他們的cookie數據時都可以搜索你定義的變量。

if ($this->input->post('rememberMe')) 
{ 
    $this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure); 
} 

如果您不想使用變量,則可以將數組傳遞給數組。一旦你做到了這一點,挑一個名字的cookie,並設置用戶名在$值,或任何你想要的,然後在控制器的登錄頁面的樣子的cookie:

if($this->input->get_cookie('cookie_name')) 
{ 
    //set the username and password for your form 
} 
else 
{ 
    //No cookie was found. Don't set anything. 
} 

我不今天早上沒有時間來測試這個,但它應該適合你。

+0

我的能力讓簡單的解決方案變得複雜化,有時讓我感到驚訝。 :) 謝謝。 – Seth

0

這樣做:

// Change the config setting 
$this->config->set_item('sess_expiration',0); 

// Force the Session class to recapture global settings by calling it's constructor 
$this->session->CI_Session() 
+0

當我運行'CI_Session()'代碼是打破我的認證控制器。我應該先設置'$ user'會話信息,還是在我調用'CI_Session()'函數之後?我無法在用戶指南中找到它。 – Seth