Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter sessions vs PHP sessions

I'm relatively new to CodeIgniter and am making my first CI project in which there are user-accounts, etc. In the past, I have always used PHP's $_SESSION variable to this end. However, CI seems to have its own session mechanism, which it claims is "better"

CI's session mechanism seems to store all the data in a cookie? Personally I like the idea of all the data being stored on the server, accessed with a cookie-key like PHPs native session mechanism... Am I being dumb thinking that's better? Should I just accept CI's mechanism? Or should I go ahead and use native PHP sessions?

What do you guys do?

Thanks,
Mala

like image 858
Mala Avatar asked Jan 05 '10 11:01

Mala


People also ask

What is session in PHP 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.

When should you use PHP sessions?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

Is PHP session reliable?

Sessions are significantly safer than, say, cookies. But it is still possible to steal a session and thus the hacker will have total access to whatever is in that session. Some ways to avoid this are IP Checking (which works pretty well, but is very low fi and thus not reliable on its own), and using a nonce.

How many types of sessions are there in PHP?

In the PHP session lifecycle, there are different stages like open, read, write, and close. Additionally, there are two more stages: destroy and garbage collection.


1 Answers

In my experience with CI I've encountered some anomalies with its sessions, but for most day-to-day needs the library is good and easy to work with. As it was pointed out, Flashdata is a very nice feature.

If you choose to stay with CI's sessions, I'd strongly suggest to store sessions in a database and, additionally, encrypt cookies:

$config['sess_encrypt_cookie'] = TRUE; $config['sess_use_database']   = TRUE; $config['sess_table_name']     = 'sessions'; 

The database structure should be as follows:

CREATE TABLE IF NOT EXISTS  `sessions` (     session_id varchar(40) DEFAULT '0' NOT NULL,     ip_address varchar(16) DEFAULT '0' NOT NULL,     user_agent varchar(50) NOT NULL,     last_activity int(10) unsigned DEFAULT 0 NOT NULL,     user_data text NOT NULL,     PRIMARY KEY (session_id) ); 
like image 56
Cinnamon Avatar answered Oct 02 '22 15:10

Cinnamon