Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which session library should I use with CodeIgniter?

I have recently started using CI and with it CI sessions, but I have noticed that one thing in particular is much more time consuming to do with CI sessions than with the base PHP sessions: Arrays.

I have an array of data that persists regardless of login/logout called $_SESSION['stats'], I then store data in that array in the form:

$_SESSION['stats']['last_page'] = $_SERVER['REQUEST_URI'];.

And when a user logs out, it saves the stats array in a variable, clears the session, and then loads it back into the new session.

The problem is that in order to edit the last_page key, instead of the one line above, I have to use this code:

$stats = $this->CI->session->userdata('stats');
$stats['last_page'] = $_SERVER["REQUEST_URI"];
$this->CI->session->set_userdata('stats', $stats);

This is one of a number of annoyances I find in CI sessions, which cause me to feel dissatisfied with it as my session handler. So my question is: Which session system should I use with CodeIgniter?... is there some reason for using CI sessions? Is there a CI library that you would suggest? Why not just use PHP sessions?

Thanks,

Lemiant

like image 788
lemiant Avatar asked Sep 07 '10 23:09

lemiant


People also ask

Which method is used to set session in CodeIgniter?

Sessions data are available globally through the site but to use those data we first need to initialize the session. We can do that by executing the following line in constructor. $this->load->library('session'); After loading the session library, you can simply use the session object as shown below.

What is the use of session in CodeIgniter?

The Session class permits you maintain a user's “state” and track their activity while they browse your site. CodeIgniter comes with a few session storage drivers: files (default; file-system based) database.

How can we store multiple values in a session in CodeIgniter?

You can also use the set_userdata method to create multiple variables in a single call. In that case, you just need to provide one argument, and it should be an array as shown below. ); $this ->session->set_userdata( $arraydata );


1 Answers

CI sessions offers some extra functionality; such as auto regenerating the session id every given amount of time (for security), IP address tracking, and flashdata (session data that's cleared after it's read once).

CI's session mechanism stores all the data in a cookie. PHP's native session mechanism is stored server side. Each have it's advantages/disadvantages. Cookies can only hold 4KB of data, so if your storing large amounts of data in session PHP native sessions might be better.

If you decide to you want to use native PHP sessions use: Session Hybrid (CI 1.7.2)

Session Hybrid uses native PHP sessions, can store session data in the default CI db, is a drop-in replacement for CI’s session class, and only requires one file to be rewritten.

[* If using a CI version before 1.7.0 try PHPSession and Native Session]

Side note: If you choose to stay with CI's sessions, for additional security you can store sessions in a database and encrypt the cookies (see Session Preferences).

like image 101
Mitchell McKenna Avatar answered Oct 12 '22 18:10

Mitchell McKenna