Codeigniter Session Database

I'm trying to create a system that remembers the user’s interaction with the site, for example, my site allows users to create their own navigation system, but I want the system to be able to remember the navigation system that they choose, without the user having to register, I assume that for this I I need to use sessions / cookies, and besides, I would suggest that I would need to use cookies as they do not expire when the browser closes (I know that they expire after a while) ,

So, I installed codeigniter using the session library and saved the session identifiers in the database. What I need to know is how to use sessions and cookies to preserve the user's navigation choice, for example, if the user selects the user to navigate blogs, then I need to save this so that the next time they come to the site, used. Can someone point me in the right direction? Please do not call me in the manual. I tried the cookie helper and all I try is a cookie will not be set.

+5
source share
3 answers

, , . cookie, , , sessions . , , 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;

, .

+3

, :

$this->session->unset_userdata('navigation_choice_a');
+1
  • When a customer selects a navigation system, you need to save the navigation selection for customers in the database.

  • Use login.

  • Retrieve data from the database.

I display client information like this in the 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 username

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

All Articles