Codeigniter - Avoiding Database Caching for Sessions

I am developing a website (with a shopping cart) on top of Codeigniter and want to use the sess_use_database parameter to make it difficult for users to hack into the shopping cart session.

I also want to use database caching to speed up common database queries (for example, β€œget categories”, since most of the contents of the database will not change regularly), so I turned on this option:

$db['development']['cache_on'] = TRUE; //where 'development' is my environment 

As a result, I found that the contents of the session are not being updated, for example, with this request:

 $this->basket_contents = array_values($this->session->userdata('basket_contents')); 

Also, I tried this:

 $this->db->cache_off(); 

... before requesting a session, but it does not solve the problem (I assume, because this is not a direct DB request).

My session settings are as follows:

 $config['sess_cookie_name'] = 'str_session'; $config['sess_expiration'] = 7200; $config['sess_encrypt_cookie'] = FALSE; $config['sess_use_database'] = TRUE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300; 

Is it possible to prevent caching of session-related database queries? Or to prohibit caching of certain tables?

OR is there another (possibly obvious) solution that I haven't thought about?

Thanks in advance.

+6
php caching session-cookies codeigniter
source share
2 answers

In CodeIgniter, caching is an all-or-nothing solution; you cannot enable it for individual tables.

There are two options that I can think of:

  • don't use CI-shared caching, but use a cache library like http://philsturgeon.co.uk/code/codeigniter-cache
  • hack db_driver.php. Line 265 (CI 1.7.2) checks whether caching is enabled. Change this row so that it does not run when it was selected in the session table.
+3
source share

Up to version 2.2.1 there may be a simple solution:

in file system / libraries / Session.php

find the line with the line "$ query = $ this-> CI-> db-> get ($ this-> sess_table_name);

  • before putting the line:

     // saving cache_on $cache_on = $this->CI->db->cache_on; $this->CI->db->cache_on = false; 
  • after the line:

     // restoring cache_on $this->CI->db->cache_on = $cache_on; 

This will prevent mysql caching for this exact session request, leaving your cache logic as it is.

Remember that this change applies to the file system file, so if you upgrade CI to the top version, this hack will be lost.

+1
source share

All Articles