Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 708
  • Last Modified:

CodeIgniter Session Not Persisting

I am having a problem where I am able to successfully set (and retrieve) session data in my login controller, but when I forward a user to on to the destination page, the session data that I set is not being found. I have researched and found others saying that playing around with the cookie domain in the config file has helped them, but it doesn't seem to do anything for my problem. If it helps for troubleshooting, I'm using an HTTPS site with the cookie_secure set to TRUE and have tried both a blank domain and ".mydomain.com" and it doesn't seem to help. sess_encrypt_cookie is TRUE and sess_use_database if FALSE. I have verified that the system clock is set correctly as well.

Why isn't my session persisting?!
0
dale_abrams
Asked:
dale_abrams
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Do you have 'session_start();' at the top of every page?   http://php.net/manual/en/function.session-start.php
0
 
dale_abramsAuthor Commented:
Hi Dave. I'm using CodeIgniter. It has a build in mechanism for handling sessions. I did forget to include that I have $autoload['libraries'] = array('session'); set in my autoload file. That is the equivalent of having session_start() at the top of every page.
0
 
Dave BaldwinFixer of ProblemsCommented:
I don't use Codeigniter so I'll to take your word for that.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Marco GasiFreelancerCommented:
Please, what is the code you're using to access session data? And have you tried to use var damp to check the content of the session?
0
 
Loganathan NatarajanLAMP DeveloperCommented:
Just verify whether you have set the session value correctly as per CI syntax and try to print on the same page to check.
Then on the destination page enable session library and print it. It should work.

More details here,  CI Sesssion
0
 
dale_abramsAuthor Commented:
After banging my head against a wall for a week now, I have figured out my issue. All of the CI session configuration and usage was correct for the record. What was happening was that the cookie size was going past the (seemingly arbitrary) browser 4kb limit and the cookie was getting wiped out by the browser before the next page load. To fix this, I moved my session to be stored in the database. After doing this, I found a further issue with the recommended ci_sessions table config that CI provides in the documentation. They give a user agent field with a length of 120, which also caused problems and in turn created a new session every time a page (or AJAX request) loaded. So, to try and help the next guy with this same issue, here is the ci_sessions table creation SQL that will work if you're finding that your session data is too large...
DROP TABLE IF EXISTS `ci_sessions`;
CREATE TABLE IF NOT EXISTS  `ci_sessions` (
	session_id varchar(100) DEFAULT '0' NOT NULL,
	ip_address varchar(100) DEFAULT '0' NOT NULL,
	user_agent varchar(300) 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`)
);

Open in new window

0
 
dale_abramsAuthor Commented:
Other solutions were not helpful in pointing me in the right direction to fix the issue I was encountering. So, I accepted my own solution in the hopes that it will be found by someone else with the same issue and save them the headache.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now