I’m new in WordPress development and I just created my first theme.
In my wp-admin, the "Site Health"
tells me that a PHP session has been detected (critical error), here is the message :
A PHP session was created by a session_start() function call. This
interferes with REST API and loopback requests. The session should be
closed by session_write_close() before making any HTTP requests.
I need PHP’s $_SESSION
for a theme script, and I added this to my functions.php file for sessions to be properly initialized:
<?php
if (!session_id()) {
session_start();
}
If I delete these lines, the message disappears, but my PHP sessions don’t work anymore.
If I keep these lines, everything seems to be working properly but this message is worrying…
Does anyone have an idea to solve this problem while keeping the ability to use the $_SESSION
?
My WP version is 5.5.3
and the PHP version is 7.4.8
.
Thank you in advance for your help!
4 Answers
I was facing same issue.
I replaced the code
session_start();
with
if (!isset($_SESSION)) {
session_start(['read_and_close' => true]);
}
and it worked for me.