0

I am developing a e-commerce website in codeigniter having sessions for following:

  1. admin login
  2. shopping cart
  3. user login

Now i am unable to figure out a mechanism for managing these three sessions with different sets of data.

I know it can be achieved with session_name() in core php. But with codeigniter i am a bit confused. Googled it a lot but couldn't found a proper answer.

I want a clear understanding of multiple sessions in codeigniter so that it doesn't create any confusion in future. Any link to tutorial would be great.

4
  • i want to use native PHP sessions Commented Aug 22, 2012 at 18:59
  • theres a code igniter library for native sessions if you google for it. Not sure how well it works. Regular code igniter sessions are done with a cookie limited to about 4k in size which breaks everything when you go over the size limit. You can use db_sessions as well by changing config option which removes the size limitation. Commented Aug 22, 2012 at 19:05
  • well i really want to use native PHP Sessions instead of the library provided by codeigniter. I know this can be done but not the specifics. Commented Aug 22, 2012 at 19:24
  • why would you want "multiple" sessions? Just have one session with all the data in it?? Commented Aug 22, 2012 at 19:52

4 Answers 4

1
$this->session->set_data('admin',$array_of_admin_data);
$this->session->set_data('user',$array_of_user_data);
$this->session->set_data('cart',$array_of_cart_data);

then retrieve each seesion data ? using

$this->session->userdata('admin');

why wouldnt this work ?

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the reply. But i do not want to use the codeigniter sessions as they store the data using cookies which forces me to think about the security concerns.
1

Codeigniter's session is completely different from Native PHP Session and it's very secure especially when stored in a database. You can work with codeigniter's sessions and Native PHP Session they would work fine together because PHP would see the as different variables. If you want to check for the existence of a codeigniter session value use

if(isset($this->session->userdata['course_id']))
{
    $this->data['course_id'] = $this->session->userdata('course_id');
} 

While Native PHP would look like

if(isset($_SESSION['course_id']))
{
    $this->data['course_id'] = $_SESSION['course_id'];
} 

Comments

1

I'm also developing a e-commerce system and have the same problem. I didn't like to manage all the data stored as serialized data in the database, especially not for the cart/basket, as it makes it hard to query the database and i simply think it's kinda messy.

I'm quite new to CI and stackoverflow, but my idea was to set up the ci_session table as it is written in the docs, thus keeping the consistency and security of CI sessions. Then i only add one reference id to the session varialbe for each session data you want to manage. E.g. for the cart:

$unique_cart_id = random_string('unique');
$this->session->set_userdata('cart_id', $unique_cart_id);

and now i reference my cart_id in a separate "basket"-table with all the data. E.g.:

CREATE TABLE basket (
  id int(11) NOT NULL auto_increment,
  cart_id varchar(32) default NULL,
  timestamp timestamp NULL default NULL,
  price float(10,2) default NULL,
  title varchar(100) default NULL,
  serialized_data text,
  product_id int(11) default NULL,
  PRIMARY KEY  (id)
)

Now each time someone wants to add something to the basket i get the cart_id from the session variable:

$cart_id = $this->session->userdata('cart_id');

and store the $cart_id together with any other cart data in my "basket"-table, by writing a little insert script.

$data = array(
   'cart_id' => $cart_id,
   'title' => $product_title,       
   'product_id' => $product_id,
   'price' => $price,
   'serialized_data' => serialize(array('what ever you need else'))
);

$this->db->insert('basket', $data); 

Now if someone wants to view the basket I just retrieve the $cart_id from the session variable again and query the database where it matches the cart_id.

$this->db->query("SELECT * FROM basket WHERE cart_id = ?", array($cart_id));

And you use the same principle for the your other session data, admin and user login. Thus I keep only to have a single session cookie for each user but I can manage each type of session data separately in the database.

This was just a rough breakdown of the idea. I hope it helped.

Comments

-1

With the userdata() method, you can retrieve the fields you saved during the creation of the session.

For example, you pass the $array_of_admin_data array, which holds some information like userType. If you want to retrieve this value, you must call userdata() like this:

$this->session->userdata('userType');

Hope, that this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.