CI's session handling not working in WebKit browsers and IE
Data stored using CodeIgniter's session library is not persisted between requests in browsers other than Firefox.
To recreate this problem install a fresh copy of CI 2.1.0, add an encryption key to application/config/config.php, then add the following three lines to the constructor of application/controllers/welcome.php:
$this->load->library('session');
$session_id = $this->session->userdata('session_id');
echo "My Session ID is $session_id";
Viewing this page in a browser should display the current session id and this id should remain the same if the page is reloaded. However, in Safari, Chrome, and IE reloading the page causes the session id to change. Something in the way CodeIgniter stores session data is triggering the session id to change which means any data stored in the session cannot be persisted from one request to the next.
Please note that no changes were made to the default CI installation other than those mentioned above. This means that the database schema which was changed in CI 2.0.3 is not the cause of this issue. Furthermore, if logging is enabled then the following entry is made to the log file after instantiating the database driver class
DEBUG - 2011-12-06 00:35:01 --> A session cookie was not found.
Since the default session setup does work in Firefox (version 8 at least) then the cause of this problem is probably related to the user agent value.
I've spent a better part of the day trying to figure out why I am getting tons of Session inserts in my db every time I refresh the screen, I tore my code apart and put it back together, all to find out it's CI 2.1.0
Oh thank goodness this is a known issue. I was going crazy thinking my server config was incorrect.
Same happened to me.
Out of curiosity I have done exactly what the @jgdev said (fresh CI 2.1.0 - insert passphrase - add lines to welcome controller (index method)). Yet the session-id remains the SAME.
Perhaps this has to do with Server/PHP versions. Cannot reproduce on: Win7 (64bit) + Apache (2.2.21) + PHP 5.3.8 Tested with: Firefox + IE9 + Chrome
Perhaps OP and other guys in this thread can say what they use as OS and Environment?
Apache: Server version: Apache/2.2.20 (Ubuntu) Server built: Nov 7 2011 22:48:05
OS: Ubuntu 11.10 Kernel: 3.0.0-14-generic-pae
PHP: PHP 5.3.6-13ubuntu3.3 with Suhosin-Patch (cli) (built: Dec 13 2011 18:37:10) Copyright (c) 1997-2011 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
Opera version: Opera 11.60 Build 1185 for Linux i386.
MySQL: mysql Ver 14.14 Distrib 5.1.58, for debian-linux-gnu (i686) using readline 6.2
Only happens with Opera. In firefox and Chrome Works fine.
It creates a new session every time I reload the page.
My previous related problems with cookies could always be traced down to 1 single line in the config:
$config['cookie_domain'] = "";
For example, when you put here 127.0.0.1 (as the domain should have at least one . (dot) ).. then the cookie wont work when I use http://localhost/ci .. simply because 127.0.0.1 and localhost are not the same domain. Ofc the cookie will work if I had used: http://127.0.0.1/ci :D So if you set here an URL.. then the direct IP will not work and vice versa. In other words, please make sure the domain match up to the url you are using to access CI.
If you leave the cookie domain empty... it seems to work on all browsers (IE9 / FF8 / Chrome / Opera11.10) with direct IP and URLs (tested on Win7 / apache2.2.21 / php 5.3.8) But an empty domain could lead to unpredictable behavior (for example if domain is required by browser, or something like that)... I think it is always a good idea to specify the cookie-domain.
Hopefully this helps a bit.. ^^
I have the cookie domain correctly configured (127.0.0.1) and I use that IP to access the app.
Hi guys, I have the same problem. I agree with the theory about is a user agent problem. I find a solution for me, in the config.php I changed that line:
$config['sess_match_useragent'] = TRUE;
to this one:
$config['sess_match_useragent'] = FALSE;
It works for me, now id session is the same.
But that suposes I will not have the posibility to add that security layer.
I'm now having the issue in Firefox too, is this ever going to get fixed????????????
I can't use userdata for session authentication, so annoying.
Does that happen if sess_use_database is set to FALSE?
Edit: I mean - I know you can't check for multiple database entries that way, but is the session data retained between requests?
When I try to log in with Opera, and sess_use_database = FALSE; I get this:
The action you have requested is not allowed.
@Razican That's triggered from the Security library and it should happen only if you are submitting a form.
Yes, it happens whenever I try to log in. If I do not login How can I check if session is being regenerated?
But, why does it no say nothing when I submit the form in other browser but it does when submitting it in Opera?
Usually you can just put this in some controller and refresh the page afterwards:
var_dump($this->session->userdata('session_id'));
I'm not sure if you don't have to manually load the session library though.
If you still want to check if it logs you in, you can disable csrf_protection and then try again.
OK. The session is being regenerated on every request.
Could probably be unrelated but ... do you guys have ajax calls in your apps?
Not in that page, and, It also happens in other apps with no AJAX.
OK - I did some testing on the environment described by @Razican here, although really only the browser matters. It only appears with Opera for me, but I guess different browsers on different operating systems matter as well.
The problem can appear whenever the values of sess_cookie_name, cookie_prefix, cookie_domain, cookie_path and/or cookie_secure are changed as those are the factors upon which the browser decides whether to send a cookie or not. I might be wrong about all of the settings having effect, but sess_cookie_name and cookie_prefix are for sure.
Some browsers send only the first cookie found that matches these settings. So if the client already has one matching cookie and those parameters are changed - the new one doesn't replace the old one, but a new one is created. Then ... it's up to the browser's algorithm to decide which one to send and if it's not the newest one - well, we already know what happens.
Similar behavior can be expected with AJAX (XHttpRequest) calls even if a browser sends all matching cookies in a regular HTTP request, although in there it's probably related to the browser not accepting cookies from the response - it's why I asked earlier about that.
The solution would be to erase old cookies prior to sending new ones, but since the cookie name is determined by configuration values (before receiving cookies from clients) and also custom cookies not related to the Session library could've been created by the developer (or another application within the cookie_path) - we can't always be sure of what to try to delete and in some cases we can't probably even guess.
What we can do is to add another two configuration directives:
sess_cookie_ajax_update- if set toFALSEwould preventCI_Session::sess_update()from changing the session_id during an AJAX request.sess_cookie_delete_invalid- if set toTRUEwill try to delete any received cookie that doesn't match our cookie name, before sending a new one.
I can submit a pull request for that, but let me know what you think first ... any suggestions, comments, etc.?
This should be closed