简体   繁体   中英

Codeigniter Session database

I am trying to build a system the remembers the user's interactions with a website, for example my site allows the users to build their own navigation system, but I want the system to be able to remember the navigation's system they choose without the user having to sign up, I assume I need to use sessions/cookies for these, and further I would assume I would need to use cookies as they don't expire when the browser closes (I know they expire after a period of time).

So I have set up using the codeigniter session library and have the session ids saving to the database. What I need to know is how can using sessions and cookies save the user navigation choice, for example if the user chooses to user the blog navigation then I need to be able save that so next time they come to the site, the blog navigation is used. Could someone please point me in the right direction? Please don't point me at the manual. I have tried the cookie helper and whatever I try, the cookie will not set.

I know you asked not to be pointed at the manual but it really will give you the answers. You shouldn't need to interact directly with a cookie to do what you want to do, sessions handle this for you. So long as you are not saving any sensitive data, you can leave the session settings at their default which will save the session data to a cookie on the user's machine but you will want to make a small adjustment to ensure the timeout is extended.

So first things first, read: Session Class : CodeIgniter User Guide

Then you can load the session library:

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

And save data to the session:

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

Then read it out later using:

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

You can also save numbers, classes and arrays to the session and they will reconstruct again when reading the data.

One last thing, to ensure that the session doesn't expire after two hours, in your config, change the line with $config['sess_expiration'] to be:

$config['sess_expiration'] = 0;

This will ensure the session does not expire.

要清除我们使用的会话:

$this->session->unset_userdata('navigation_choice_a');
  1. When the customer chooses a navigation system, you need to save the customers navigation choices in database.

  2. Use log in.

  3. Pull the data from database.

I pull out customer info like this in a controller.

...
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'); 

This is my login controller/login

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');  
  }

And logout

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

    //destroy the session
    session_destroy(); 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM