CodeIgniter Forums
Do not use the CI 4 session library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Do not use the CI 4 session library (/showthread.php?tid=90479)

Pages: 1 2


Do not use the CI 4 session library - ElTomTom - 03-25-2024

Today I have the PHP Session with Redis configuration, with fallback, name configuration, etc., all defined in my php.ini file, however in CI4 I cannot use PHP's native session_start() to create my sessions.


In CodeIgniter 3 I could just remove it from autoload and that's it, but now it's not possible.

Every time I try to use session_start() this message appears. Suggesting that CodeIgniter automatically initialized the session. Something I don't want.


Code:
ini_set(): Session ini settings cannot be changed when a session is active

If I execute the session()->close(); below before session_start(), another error appears.

Code:
ini_set(): Session ini settings cannot be changed when a session is active

SYSTEMPATH/Session/Handlers/FileHandler.php at line 72

How can I focus on using only session_start(), even though I know it is more archaic.


RE: Do not use the CI 4 session library - kenjis - 03-25-2024

It is the following code:
https://github.com/codeigniter4/CodeIgniter4/blob/b2d01973767b0b460b98e095e5c4856c38d369bd/system/CodeIgniter.php#L1068-L1074

Extend the CodeIgniter class and override the storePreviousURL() method.
See https://codeigniter4.github.io/CodeIgniter4/extending/core_classes.html


RE: Do not use the CI 4 session library - joho - 03-26-2024

(03-25-2024, 07:14 AM)ElTomTom Wrote: Today I have the PHP Session with Redis configuration, with fallback, name configuration, etc., all defined in my php.ini file, however in CI4 I cannot use PHP's native session_start() to create my sessions.

[..]

Just out of curiosity, because I use PHP session settings geared for Redis too, what is it with CI4 sessions that doesn't work with PHP sessions using Redis?


RE: Do not use the CI 4 session library - ElTomTom - 03-28-2024

(03-26-2024, 04:04 PM)joho Wrote:
(03-25-2024, 07:14 AM)ElTomTom Wrote: Today I have the PHP Session with Redis configuration, with fallback, name configuration, etc., all defined in my php.ini file, however in CI4 I cannot use PHP's native session_start() to create my sessions.

[..]

Just out of curiosity, because I use PHP session settings geared for Redis too, what is it with CI4 sessions that doesn't work with PHP sessions using Redis?
I don't use it because of the lock it applies to sessions via Redis.
This is most evident in AJAX requests. At least in CI 3, a page that had about 5 requests took a long time for the page to finish loading all the information, as each page remained locked for 1s. In other words, in total I had 5s loading the data to be able to work.

On a large scale, this generates other problems, such as too many connections in the database
Imagine you open 5 req, each one opens a database connection. They will remain open for an average of 5 seconds for a page/user.

Now you have 10 users, making the same request, there are 50 active requests (10 users x 5 req) in the database that cannot be used by other people.


RE: Do not use the CI 4 session library - joho - 03-28-2024

(03-28-2024, 06:57 AM)ElTomTom Wrote:
(03-26-2024, 04:04 PM)joho Wrote: Just out of curiosity, because I use PHP session settings geared for Redis too, what is it with CI4 sessions that doesn't work with PHP sessions using Redis?
I don't use it because of the lock it applies to sessions via Redis.
This is most evident in AJAX requests. At least in CI 3, a page that had about 5 requests took a long time for the page to finish loading all the information, as each page remained locked for 1s. In other words, in total I had 5s loading the data to be able to work.

On a large scale, this generates other problems, such as too many connections in the database
Imagine you open 5 req, each one opens a database connection. They will remain open for an average of 5 seconds for a page/user.

Now you have 10 users, making the same request, there are 50 active requests (10 users x 5 req) in the database that cannot be used by other people.

Ah, yes, I can see where that becomes unbearable. I hadn't thought about that situation. Interesting.


RE: Do not use the CI 4 session library - kenjis - 03-28-2024

Session without lock has issues on race conditions. If it is not read only session, session data may be lost.
Read https://codeigniter4.github.io/CodeIgniter4/libraries/sessions.html#a-note-about-concurrency

The current Lock Retry Interval in CI 4.4.6 is 0.1 seconds, not 1 second. And in 4.5.0, you will be able to configure:
https://github.com/codeigniter4/CodeIgniter4/pull/8578/files#diff-e0c73b21d04b461f6ec7d761d62590136579c9feeb01b7b7baf2fc2d7cd2f5f4R113


RE: Do not use the CI 4 session library - joho - 03-28-2024

Most excellent news Smile


RE: Do not use the CI 4 session library - ElTomTom - 04-10-2024

(03-28-2024, 03:27 PM)kenjis Wrote: Session without lock has issues on race conditions. If it is not read only session, session data may be lost.
Read https://codeigniter4.github.io/CodeIgniter4/libraries/sessions.html#a-note-about-concurrency

The current Lock Retry Interval in CI 4.4.6 is 0.1 seconds, not 1 second. And in 4.5.0, you will be able to configure:
https://github.com/codeigniter4/CodeIgniter4/pull/8578/files#diff-e0c73b21d04b461f6ec7d761d62590136579c9feeb01b7b7baf2fc2d7cd2f5f4R113

Just to confirm.
If I define a Redis or Memcached session in CI 4. php.ini will be completely ignored and whatever is in my session file will be applied, correct?


RE: Do not use the CI 4 session library - kenjis - 04-10-2024

@ElTomTom Strictly speaking, no.
But the all settings in CI4's Session config file are applied.
So it seems you don't need to worry about Session settings if you set in CI4's Session config file correctly.

But there are so many settings:
https://www.php.net/manual/en/session.configuration.php
and CI4 Session does not touch some settings at all.
That is the settings are applied.


RE: Do not use the CI 4 session library - ElTomTom - 04-29-2024

@kenjis My PHP.ini is configured to use Redis. If in the CodeIgniter settings I use FileHandler, it will ignore everything defined in PHP.ini, correct?