0
votes

I'm working on a project with CodeIgniter. Because CI Session class is using Cookie to store data, and Cookie can hold only 4KB of data, so I decided to use Native PHP Session for storing data instead.

My problem is: sometimes the data stored in Session lost but I dont have any ideas about what is causing it.

Let me explain what i've done in more detail:

  1. In product detail page : Customer decides to buy this product -> He/she clicks on "Add to cart" button -> Product's information will be stored in Session. Now let's click "Next step" button

  2. Go to "View cart" page: I use data stored in Session (product_id, for example) and get some extra data from database to display for customer. Now let's click "Next step" button

  3. In this step, customer has to leave his/her informations (name, phone, email, address) for delivery and I save them in a Session variable names $_SESSION['customer_info']. Now let's click "Next step" button

  4. In this step, customer will select a payment method, review his/her order detail and delivery information (it means SESSION is working). Let's click on "Checkout" button and move to final step

  5. When customer hits "Checkout", I call an AJAX to POST the payment_method to, for example, "frontend/cart/checkout" function.

In this function, I pull out the informations stored in SESSION (cart items, customer information) and save them to the database and then send an email about the order detail to customer. The email content contains these informations : products (quantity, amount, name,...) and customer's delivery information (name, address, email, phone). I also save the email content to the database (just for make sure that I wont lose any information).

The problem is : Sometimes (just in sometimes), the delivery informations is empty (I mean there's nothing about the delivery information in the email, or even in the database, it's totally empty) but the cart items are still there. :( It's really really weird to me.

Any ideas or suggestions?

Thanks in advance!

2
do you have a load balancer?tpae
@tpae : I'm sorry but I (really) don't get it :( ? Would you mind to explain it?Nấm Lùn

2 Answers

1
votes

The solution in your case is to store session in database also making some config settings in config.php.


Steps:

1) For saving session data in database you have to create a table in your database ci_sessions. Following is the query (MySQL) for creating it,

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
   session_id varchar(40) DEFAULT '0' NOT NULL,
   ip_address varchar(45) DEFAULT '0' NOT NULL,
   user_agent varchar(120) NOT NULL,
   last_activity int(10) unsigned DEFAULT 0 NOT NULL,
   user_data text NOT NULL,
   PRIMARY KEY (session_id),
   KEY `last_activity_idx` (`last_activity`)
);


2) Once you have created your database table you can enable the database option in your config.php.

$config['sess_use_database'] = TRUE;

Now the session data will get stored into the database.


3) Also make sure you've following config settings:

$config['sess_cookie_name']     = 'ci_session';   // cookie name
$config['sess_expiration']      = 0;              // 0 means non-expiring session
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;           // set TRUE to use database
$config['sess_table_name']      = 'ci_sessions';  // DB table name
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  =  86400 * 30;    // 30 days


Usage of Session class:

Also I saw you are still using $_SESSION[''] to store or retrieve session data. Kindly follow the session class of Codeigniter.

1) Add session data.

$this->session->set_userdata('some_name', 'some_value');


2) Retrieve session data.

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


3) Retrieve all session data (returns an array of session data).

$this->session->all_userdata();


4) Removing session data.

$this->session->unset_userdata('some_name');


5) Destroying a session.

$this->session->sess_destroy();


Follow documentation:

http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

1
votes

There is a probability that PHP will clean your session data by the garbage collection of the built-in session function. If you have thousands or even more visitors everyday, you will observe your session data lost easier.

Just a hint, might http://php.net/manual/en/session.configuration.php#ini.session.gc-divisor would help.