Welcome Guest, Not a member yet? Register   Sign In
ErrorException: ini_set() A session is active.
#1

I'm getting the following error with CI4. Note that the site I'm dealing with is a combination of pure php and CI. I also only have 1 page in CI4, as I'm just starting to 'migrate' to it.

Code:
ErrorException
ini_set(): A session is active. You cannot change the session module's ini settings at this time

SYSTEMPATH/Session/Handlers/FileHandler.php at line 319
319             ini_set('session.sid_length', (string) $SIDLength);

Backtrace:

1. {PHP internal code}   —  CodeIgniter\Debug\Exceptions->errorHandler ( arguments )

2. SYSTEMPATH/Session/Handlers/FileHandler.php : 319   —   ini_set()

3. SYSTEMPATH/Session/Handlers/FileHandler.php : 83   —  CodeIgniter\Session\Handlers\FileHandler->configureSessionIDRegex ()

4. SYSTEMPATH/Config/Services.php : 686   —  CodeIgniter\Session\Handlers\FileHandler->__construct ( arguments )

5. SYSTEMPATH/Config/BaseService.php : 258   —  CodeIgniter\Config\Services::session ( arguments )

6. SYSTEMPATH/Config/BaseService.php : 199   —  CodeIgniter\Config\BaseService::__callStatic ( arguments )

7. SYSTEMPATH/Config/Services.php : 664   —  CodeIgniter\Config\BaseService::getSharedInstance ( arguments )

8. SYSTEMPATH/Config/BaseService.php : 258   —  CodeIgniter\Config\Services::session ()

9. SYSTEMPATH/Common.php : 996   —  CodeIgniter\Config\BaseService::__callStatic ( arguments )

10. SYSTEMPATH/CodeIgniter.php : 1069   —   session()

11. SYSTEMPATH/CodeIgniter.php : 549   —  CodeIgniter\CodeIgniter->storePreviousURL ( arguments )

12. SYSTEMPATH/CodeIgniter.php : 361   —  CodeIgniter\CodeIgniter->handleRequest ( arguments )

13. FCPATH/index.php : 114   —  CodeIgniter\CodeIgniter->run ()

To simplify things, essentially what I'm running into is that my controller loads my header helper where, among other things, I check if the session is valid (ie. user is logged in), which needs to call session_start() to check variables. Anyway, because of this, the page fails to load. If I place an "exit();" at the bottom of my view page, it will load just fine. If I let it go through as normal, it will fail with the above. As a workaround, I can add the '@' in front of 'ini_set()' to disable errors, but I'd like to figure out how to fix it properly.

I figure I'm either doing something wrong, or I found a potential bug, but I'd bet on the former...

Anyone have any thoughts on what I should do to fix this?
Reply
#2

What is your header helper ?
You must not call PHP header() function in CI4 app.
All headers are managed by CI4 Response object.
Reply
#3

(01-26-2024, 08:53 PM)kenjis Wrote: What is your header helper ?
You must not call PHP header() function in CI4 app.
All headers are managed by CI4 Response object.

My header helper literally just loads a few pure php files.

header.php
--> starts php session, so it can check whether or not user is logged in; redirecting user if not logged in
--> loads some javascript functions
--> displays navigation menu's that are the same for all pages

common_functions.php
--> just a bunch of php functions that are used throughout the site
Reply
#4

(01-26-2024, 09:05 PM)berky Wrote: My header helper literally just loads a few pure php files.

header.php
--> starts php session, so it can check whether or not user is logged in; redirecting user if not logged in
--> loads some javascript functions
--> displays navigation menu's that are the same for all pages

You start PHP session. So you cannot use CI4 Session class at the same time.
and if it displays (echo?) navigation menu, CI4 does not work properly after it.

If you use CI4, you must follow the framework.
You must not manipulate any HTTP header (including session cookie) and must not output (echo) anything.
Reply
#5

(This post was last modified: 01-27-2024, 07:01 AM by berky.)

(01-26-2024, 09:17 PM)kenjis Wrote:
(01-26-2024, 09:05 PM)berky Wrote: My header helper literally just loads a few pure php files.

header.php
--> starts php session, so it can check whether or not user is logged in; redirecting user if not logged in
--> loads some javascript functions
--> displays navigation menu's that are the same for all pages

You start PHP session. So you cannot use CI4 Session class at the same time.
and if it displays (echo?) navigation menu, CI4 does not work properly after it.

If you use CI4, you must follow the framework.
You must not manipulate any HTTP header (including session cookie) and must not output (echo) anything.

Then is there a way to write my header/login check in CI4 space, and be able to import it from my pure php side? I am trying to avoid maintaining 2 equivalent files during the transition process.
Reply
#6

(01-26-2024, 09:05 PM)berky Wrote: My header helper literally just loads a few pure php files.

header.php
--> starts php session, so it can check whether or not user is logged in; redirecting user if not logged in
--> loads some javascript functions
--> displays navigation menu's that are the same for all pages

header.php does too many things.
It can be split into three parts:
1. Session thing
2. JavaScript functions
3. displays navigation menu

3. is a part of a view. Load it in your view file.

1. is the problem. Don't call session functions in the controller.
2. has no problem. In CI context, it would be a helper file.
Reply
#7

(01-27-2024, 07:00 AM)berky Wrote: Then is there a way to write my header/login check in CI4 space, and be able to import it from my pure php side? I am trying to avoid maintaining 2 equivalent files during the transition process.

You cannot use PHP Session and CI Session at the same time.
Because CI Session is based on PHP Session. After all, they use the same PHP Session feature.
CI Session is an extension of PHP Session.

So it might be better to use CI Session in your pure PHP script.
But I don't know you can do it easily.

This shows how to use CI classes in pure PHP file.
https://github.com/codeigniter4/translat...p/bin/test
It uses CodeIgniter\CLI\CLI class.
Reply
#8

Thanks for your help. Ultimately it seems like it's going to be easier for me to just maintain multiple files until we get to the point where we can rid ourselves of the other environment. Thankfully, it appears that, since our other environment controls the login process (technically a combined CI2 [yes, it's old] and pure php), it somehow automagically creates a $_COOKIE variable with the information I need to pull from on the CI4 side to validate the login.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB