session_id function in PHP 8 is blank every other time I call it

528 Views Asked by At

I have tried both two different versions of PHP 8 and one version prior to PHP 8. Both of the PHP 8 version return a blank session_id value every other time I call it.

This was not the case prior to PHP 8.

<?php
echo session_id();
?>

I am not sure how to fix this...any pointers?

1

There are 1 best solutions below

0
Abraham Covelo On

session_id returns an empty string if there is no current session. Because session creation is not automatic.

See session_start:

https://www.php.net/manual/en/function.session-start.php

And session_id:

https://www.php.net/manual/en/function.session-id.php

In version 7.1.0 session_start changes behavior and session_id changes as well in version 8 -> id param is nullable now.