簡體   English   中英

Codeigniter會話數據庫

[英]Codeigniter Session database

我正在嘗試構建一個記住用戶與網站交互的系統,例如我的網站允許用戶構建自己的導航系統,但我希望系統能夠記住他們選擇的導航系統而無需用戶注冊,我認為我需要使用會話/ cookie,而且我認為我需要使用cookie,因為它們在瀏覽器關閉時不會過期(我知道它們會在一段時間后過期)。

所以我已經使用codeigniter會話庫進行了設置,並將會話ID保存到數據庫中。 我需要知道的是如何使用會話和cookie保存用戶導航選項,例如,如果用戶選擇使用博客導航,那么我需要能夠保存,以便下次他們來到網站時,博客導航是用過的。 有人可以指點我正確的方向嗎? 請不要指向我手冊。 我嘗試過cookie幫助程序,無論我嘗試什么,cookie都不會設置。

我知道你要求不要指出手冊,但它確實會給你答案。 您不應該直接與cookie進行交互以執行您想要執行的操作, 會話將為您處理此問題。 只要您不保存任何敏感數據,您可以將會話設置保留為默認值,這會將會話數據保存到用戶計算機上的cookie中,但您需要進行小幅調整以確保延長超時。

首先,請閱讀: 會話類:CodeIgniter用戶指南

然后你可以加載會話庫:

$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;

這將確保會話不會過期。

要清除我們使用的會話:

$this->session->unset_userdata('navigation_choice_a');
  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');    
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM