CodeIgniter Forums
codeigniter 4.1.8 - Session: Initialization under CLI aborted. - 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: codeigniter 4.1.8 - Session: Initialization under CLI aborted. (/showthread.php?tid=81265)



codeigniter 4.1.8 - Session: Initialization under CLI aborted. - magu - 02-11-2022

Hi,
I get this error "Session: Initialization under CLI aborted." in a log file, when I try to call a controller (php public/index.php tools) via cli in CI development environment. I want to allow a cronjob to call a specific route to save some data in mysql.

This is the example I used: https://codeigniter.com/user_guide/cli/cli.html#let-s-try-it-hello-world

After debugging for a while, I changed CI_ENVIRONMENT = development to CI_ENVIRONMENT = testing in the .env file and it worked. I was able to call the route via cli.

Why is calling a route via cli only allowed in testing mode? Is this a bug or is this the way it should work?

I am new to codeigniter. I hope someone can explain it. Thanks.

Greetings, magu


RE: codeigniter 4.1.8 - Session: Initialization under CLI aborted. - paulbalandan - 02-11-2022

Hi!

1) Do not ever use the "testing" environment in your projects. It is internal to the development team for our unit testing.

2) By definition, the CLI version of PHP is a stand-alone process. It's not tied in any way to a web-server, or an HTTP session, or anything like that. Consequently, the concept of GET/POST/session/cookie variables is meaningless in CLI PHP. (https://bytes.com/topic/php/answers/484626-how-do-you-acceess-_session-within-cli-php-script). In short, you really can't access Session in CLI since PHP itself allows it only in HTTP (browser) context.


RE: codeigniter 4.1.8 - Session: Initialization under CLI aborted. - engel - 11-30-2023

Maybe this is too late but in case anyone might come across to this problem, "Session: Initialization under CLI aborted." implies that your application is autoloading the session library while you are using CLI. @paulbalandan is right that you can't really access Session in CLI. In my case, I needed the Session to be auto-loaded, so I had to exclude the controller that was used for CLI access from the Before Filters. Hope this helps and still relevant.


RE: codeigniter 4.1.8 - Session: Initialization under CLI aborted. - Gary - 01-24-2024

(11-30-2023, 04:14 AM)engel Wrote: Maybe this is too late but in case anyone might come across to this problem, "Session: Initialization under CLI aborted." implies that your application is  autoloading the session library while you are  using CLI. @paulbalandan is right that you can't really access Session in CLI. In my case, I needed the Session to be auto-loaded, so I had to exclude the controller that was used for CLI access from the Before Filters. Hope this helps and still relevant.

You're brilliant Engel!  That's an excellently elegant work-around!  Thanks.