[Solved] I have an error when I write to the session on a production server

Good morning,

After developing and testing a project locally, I installed it on the production server.

But there, as soon as the application has to write in session, I get an error message:
Cake\Http\Session::_overwrite(): Argument #1 ($old) must be of type array, null given, called in /home/clients/…/vendor/cakephp/cakephp/src/Http/Session.php on line 513

Even flash component throws the error when it needs to write to the session.

I’ve looked for solutions on various forums, but so far nothing conclusive.

The best track I had was an output_buffering active locally and not in prod, but for my part it is deactivated by default locally.

My version of Cakephp is 4.2.12. The prod php is 8.1 as in local.

Here is one of the functions present in a Utility file which generates this error:

public function cookiesConsent() {
    $session = Router::getRequest()->getSession();
    
    $session->write([
        'Config.cookies_consent' => 'oui',
    ]);
}

I also have notifications, but I don’t know if they really affect this problem.

Deprecated (8192): Return type of Cake\Database\Query::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [CORE/src/Database/Query.php, line 1908]

Deprecated (8192): Return type of Cake\Database\Statement\StatementDecorator::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [CORE/src/Database/Statement/StatementDecorator.php, line 285]

Deprecated (8192): Return type of Cake\ORM\ResultSet::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [CORE/src/ORM/ResultSet.php, line 187]

Deprecated (8192): Cake\ORM\ResultSet implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) [CORE/src/ORM/ResultSet.php, line 33]

Deprecated (8192): Cake\Collection\Collection implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) [CORE/src/Collection/Collection.php, line 27]

Deprecated (8192): Cake\Collection\Iterator\FilterIterator implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) [CORE/src/Collection/Iterator/FilterIterator.php, line 31]

Deprecated (8192): Return type of & Cake\ORM\Entity::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [CORE/src/Datasource/EntityTrait.php, line 604]

Warning (512): Unable to emit headers. Headers sent in file=/home/clients/8bab20da221b641bfd7b7ea783842e6e/sites/jeu-noel-grandlitier.fr/vendor/cakephp/cakephp/src/Error/Debugger.php line=971 [CORE/src/Http/ResponseEmitter.php, line 71]

Warning (2): Cannot modify header information - headers already sent by (output started at /home/clients/8bab20da221b641bfd7b7ea783842e6e/sites/jeu-noel-grandlitier.fr/vendor/cakephp/cakephp/src/Error/Debugger.php:971) [CORE/src/Http/ResponseEmitter.php, line 168]

Warning (2): Cannot modify header information - headers already sent by (output started at /home/clients/8bab20da221b641bfd7b7ea783842e6e/sites/jeu-noel-grandlitier.fr/vendor/cakephp/cakephp/src/Error/Debugger.php:971) [CORE/src/Http/ResponseEmitter.php, line 197]

I’m open to any leads, because I’m completely stuck at the moment.

Thanks in advance,
Alexis

I’m guessing but… some things to check

Is you application sending and receiving PHPSESSID Cookie: headers to/from your browser?
image

You might want to check how your session settings are configured in config/app.php Sessions - 4.x.

What type of session is your application using in the defaults key (php, cache, database, cake)?

  # config/app.php
  'Session' => [
        'defaults' => 'php',
    ],

Is the problem that your Session is not starting because the hosting server cannot save the session file?

If you’re using php sessions what is your PHP session.save_path? Does it exist and can the webserver write to it? What are the permissions of the session.save_path folder?

To check session.save_path

# on the webserver if you have access to cli
php -i | grep session.save_path

# or
php -r 'echo ini_get("session.save_path") . PHP_EOL;'

#or
<?php 
# create a file such as webroot/info.php and browse to it
phpinfo();

You can if needed modify Session to save session files to somewhere writeable (if you move the save_path you may need to make a clean up cron script to delete stale session (e.g. sess_nq0t33hq3h2tudrcjac08lq0ar) files

  'Session' => [
        'defaults' => 'php', // or cake, or database, or cache
        'ini' =>
        ['session.save_path' => '/path/to/a/dir/in/your/webspace']
    ],

Crazy I just went to chatgpt.com and asked “what can cause a php session not to start” and it gave me a list of 10 things to check… Wow we humans are nearly obsolete.

I am not sure CakePHP 4.2 is 100% compatible wit PHP 8.1

I’d recommend going to at least CakePHP 4.4 since this version at least supports PHP 8.2 as you can see here

With that you can get rid of the deprecation warnings.


Another way to get rid of these deprecated messages is to change the error_level in your config to something like

'errorLevel' => E_ALL & ~E_NOTICE & ~E_DEPRECATED,

Then the HTTP headers can be set correctly by PHP and the session should work again.

1 Like

Good morning,

Thank you very much for your responsiveness and feedback.

Indeed, by modifying the error level, everything works perfectly.

I’m going to see about upgrading to CakePHP 4.4.

Thanks also jmcd73. I read your entire message and was about to respond when I saw Kevin’s return.

I wish you both a pleasant day.

Alexis

1 Like