views:

732

answers:

3

Hello,

I am trying to build a system the remebers the users interactions with website, for example my site allows the users to build there own navigation system, but I want the system to be able to remember the navigations system they choose with out the user having to sign up, I assume I need to user 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 the codeigniter session library and have the session id's saving to the database, what I need to know is how can using sessions and cookies save the user navigation chose, for example if the user chooses to user the blog navigation then I need to be able save that so next they come to the site, the blog navigation is used. Could some please point me in the right direction? Please dont point me at the manual I have tried the cookie helper and what ever I try the cookie will not set.

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');    
 }
shin
+1  A: 

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.

Nat Ryall
A: 

hello,

I am developing a site useing codeingniter where one user can not logged in from any other system. User can logged in from one system only. And If user will be 20 mins idle then user automatically logged out. So I have build a database where I am using a field called "loggedin_status" and value can be Y(yes) or N(no), So if an user logged in from a system then this that loggedin_status will be Y and that user can not be logged in from another system. Everything is going ok, but when user is idle 20mins in that case how I can update database(that loggedin_status). I am using codeigniter in built session class.

Please help me.

Thanks Riktam

riktam