Reputation: 403
I am upgrading an application from CodeIgniter 2.2.1 to 3.1.8. The application authenticates users and saves the authentication state in a session, and after upgrading that has broken such that users can no longer log in. I'm running PHP Version 7.1.12 via PHP-FPM on nginx, on a Linux virtual machine.
I've spent the best part of 1.5 days trying to get to the bottom of this. As best I can tell it boils down to PHP not reading the session ID that is sent in by the browser via cookie, and creating a new session (and session ID) for each request. I can tell this from the fact that the ci_session
value from the request cookie is different from the ci_session
value in the corresponding response cookie (as display in Firefox devtools), as well as the fact that a new file is created each time. (I have configured CodeIgniter to use the files driver.)
In attempting to debug, if I log the value of session_id()
before the call to session_start()
(in /system/libraries/Session/Session.php
, line 143), I just see a blank string. Immediately afterwards, I see the same new session ID that Firefox shows in the response cookie.
I've read fairly extensively through:
and tried the following remedies to no avail:
error_reporting
to E_ALL
and check the PHP error log for any messages e.g. relating to headers already having been output$config['encryption_key']
to a random 32-character string copied from the CodeIgniter section of http://randomkeygen.com/$config['sess_cookie_name']
to a string consisting solely of lower-case alphanumeric characters$config['sess_save_path']
directory, with the correct IDs and information$config['cookie_domain']
to .domainname.tld
(and also just ''
)/system
folder with the 3.1.8 version).Below I've pasted what I believe are all the relevant settings from CI and from PHP. Clearly I am missing something, quite possibly something really basic. How can I diagnose further/fix?
CodeIgniter settings
$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 3600 * 24 * 60; // 60 days
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_driver'] = 'files';
$config['sess_save_path'] = BASE . 'storage/session';
PHP settings
Session Support enabled
Registered save handlers files user
Registered serializer handlers php_serialize php php_binary wddx
Directive Local Value Master Value
session.auto_start Off Off
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.lazy_write On On
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /var/opt/remi/php71/lib/php/session /var/opt/remi/php71/lib/php/session
session.serialize_handler php php
session.sid_bits_per_character 5 5
session.sid_length 26 26
session.upload_progress.cleanup On On
session.upload_progress.enabled On On
session.upload_progress.freq 1% 1%
session.upload_progress.min_freq 1 1
session.upload_progress.name PHP_SESSION_UPLOAD_PROGRESS PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix upload_progress_ upload_progress_
session.use_cookies On On
session.use_only_cookies On On
session.use_strict_mode Off Off
session.use_trans_sid 0 0
Upvotes: 1
Views: 882
Reputation: 403
OK, I fixed this eventually. I'm not certain what the solution was, but the final change I made which fixed it was to clear from my browser all cookies relating to the site, having noticed that the $_COOKIE
superglobal did not include an entry for ci_session
(the name of the cookie as configured in $config['sess_cookie_name']
), despite it showing up in the browser request displayed in Firefox devtools.
Along the way, I also:
$config['sess_use_database']
and $config['sess_table_name']
)$config['sess_regenerate_destroy'] = FALSE;
)By the way, I also noticed that session_id()
returns nothing before session_start()
has been called, even when sessions are working properly (and the correct ci_session
value is present in $_COOKIE
). So I was wrong to interpret a blank string being returned by session_id()
as indicative of PHP failing to read the value submitted to it as part of the cookie.
Upvotes: 0
Reputation: 8964
The problem you describe is usually a bad $config
item setting.
You need to go to the Upgrading from 2.2.x to 3.0.x doc and work through it step by step. Step 6 talks about what needs to be done to update session usage. Looks to me like you are using v2.x session $config
items. There are important changes to $config
related to sessions. Look into those first. One of the primary ones is making sure that $config['base_url']
is set properly. A bad value there has all kinds of bad side effects.
You might want to look at each of the "Upgrading" documents between 3.0.0 and the current version to see if there is anything that might affect your site. There aren't a lot "step" beyond replacing the system folder for most versions. Beyond v3.0.0 there are none I can remember that affect sessions. But there are some important items to attend to none the less.
For help determining if sessions are working correctly check out this simple repository on GitHub.
Sometimes this problem, when using the 'files'
driver, it can be a permissions issue. But as you see files being created that's not it.
Upvotes: 1