Solved

CodeIgniter Session Not Persisting

Posted on 2015-02-08
7
365 Views
Last Modified: 2015-02-16
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
Comment
Question by:dale_abrams
7 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Do you have 'session_start();' at the top of every page?   http://php.net/manual/en/function.session-start.php
0
 
LVL 1

Author Comment

by:dale_abrams
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I don't use Codeigniter so I'll to take your word for that.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
 
LVL 36

Expert Comment

by:Loganathan Natarajan
Comment Utility
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
 
LVL 1

Accepted Solution

by:
dale_abrams earned 0 total points
Comment Utility
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
 
LVL 1

Author Closing Comment

by:dale_abrams
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now