2010-01-14 94 views
5

我想建立一個系統記住用戶與網站的互動,例如我的網站允許用戶建立自己的導航系統,但我希望系統能夠記住導航系統選擇沒有用戶必須註冊,我假設我需要使用這些會話/ cookie,並進一步我會假設我需要使用cookie,因爲它們不會在瀏覽器關閉時過期(我知道它們在一段時間後過期的時間)。Codeigniter會話數據庫

因此,我已經使用codeigniter會話庫進行了設置,並將會話ID保存到數據庫。我需要知道的是,如何使用會話和cookie保存用戶導航選項,例如,如果用戶選擇使用博客導航,那麼我需要在下次訪問該網站時能夠保存它,博客導航是用過的。有人能指點我正確的方向嗎?請不要將我指向手冊。我嘗試過cookie助手,無論我嘗試什麼,cookie都不會設置。

回答

3

我知道你不願在手冊中指出,但它真的會給你答案。你不需要直接與cookie進行交互來做你想做的事情,sessions爲你處理。只要您沒有保存任何敏感數據,您可以將會話設置保留爲默認設置,這會將會話數據保存到用戶計算機上的cookie中,但您需要進行一些小調整以確保超時延長。第一

所以第一件事情,讀:Session Class : CodeIgniter User Guide

然後你就可以加載會話庫:

$this->load->library("session"); 

和數據保存到會話:

$this->session->set_userdata("navigation_choice_a", "navigation_value_a"); 

再後來讀出來使用:

$this->session->userdata("navigation_choice_a"); 
// Will return "navigation_value_a" 

您還可以將數字,類和數組保存到會話中,並在讀取數據時再次重新構建。

最後一件事,以確保會議不兩小時後到期,在你的配置,改變符合$config['sess_expiration']是:

$config['sess_expiration'] = 0; 

這將確保會議不會過期。

0
  1. 當客戶選擇導航系統時,您需要將客戶導航選項保存在數據庫中。

  2. 使用日誌中。

  3. 從數據庫中提取數據。

我在控制器中提取這樣的客戶信息。

... 
if(isset($_SESSION['customer_id'])){ 
     $data['fname'] = $_SESSION['customer_first_name']; 
     $data['lname'] = $_SESSION['customer_last_name']; 
     $data['telephone'] = $_SESSION['phone_number']; 
     $data['email'] = $_SESSION['email']; 
     $data['address'] = $_SESSION['address']; 
     $data['city'] = $_SESSION['city']; 
     $data['pcode'] = $_SESSION['post_code']; 
    } 

    $this->load->vars($data); 
    $this->load->view('welcome/template'); 

這是我的登錄控制器/登錄

function login(){ 
    // $data['loggedin']=0; 
    if ($this->input->post('email')){ 
     $e = $this->input->post('email'); 
     $pw = $this->input->post('password'); 
     $this->MCustomers->verifyCustomer($e,$pw); 
     if (isset($_SESSION['customer_id'])){ 
      // $data['loggedin']=$_SESSION['customer_id']; 
      $this->session->set_flashdata('msg', 'You are logged in!'); 
      redirect('welcome/login','refresh'); 
     } 

     $this->session->set_flashdata('msg', 'Sorry, your email or password is incorrect!'); 
     redirect('welcome/login','refresh'); 
    }  


    $data['main'] = 'welcome/customerlogin';// this is using views/login.php 
    $data['title'] = "Customer Login"; 

    $this->load->vars($data); 
    $this->load->view('welcome/template'); 
    } 

和註銷

function logout(){ 
    // or this would remove all the variable in the session 
    session_unset(); 

    //destroy the session 
    session_destroy(); 

    redirect('welcome/index','refresh');  
} 
1

要清除的會議上,我們使用:

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