I am developing a e-commerce website in codeigniter having sessions for following:
- admin login
- shopping cart
- 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.
$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 ?
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'];
}
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.
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.