Welcome Guest, Not a member yet? Register   Sign In
Handling ci_session cookie outside of CodeIgniter
#1

[eluser]aidehua[/eluser]
I've just spent an afternoon figuring this out; it's probably obvious to most of you but, in case it might help someone some day, here goes:

I wanted to access the CodeIgniter session cookie from outside of CodeIgniter.

I wanted to get the session_id value, which I could use to get some user_data from the ci_sessions table.

(In case you / visiting search engines are interested: I wanted to find the user_id, so that I could configure the config_tinybrowser.php file in the tinybrowser plugin for the tiny_mce editor so that it would dynamically set the $tinybrowser['path']['image'] value to a unique folder for each user... but that's not important right now.)

Here's how I started:
Code:
$cookie = $_COOKIE['ci_session'];
$cookie = unserialize($cookie);
$session_id = $cookie['session_id'];

Armed with the session_id, I could query the database to find the user_id.

Worked fine on my development(localhost) server.

But on the testing server (a shared hosting environment) - I wasn't getting the session_id.

After way too long, I spotted that the ci_session cookie on the shared host was full of slashes, which weren't present in the cookie on the localhost server.

Not knowing much about serialize/unserialize, it took me quite a while to spot that the problem lay in trying to unserialize the cookie string when all the quotes were slash-escaped.

Simple solution: strip the slashes:

Code:
$cookie = $_COOKIE['ci_session'];
$cookie = stripslashes($cookie);
$cookie = unserialize($cookie);
$session_id = $cookie['session_id'];

Then you're in business.

The problem, of course, arose because there were different php.ini settings between the two servers: the shared host has magic_quotes switched on; my localhost does not.

Well, just thought I'd let you know where my afternoon went. If this saves anyone 10 minutes of hassle in the future, so much the better.




Theme © iAndrew 2016 - Forum software by © MyBB