Search code examples
phphtmlsessionsession-cookiessession-variables

PHP session_start returns false, $_SESSION variables are not persistent


Let me start by saying that I'm relatively new to PHP, so apologies if this seems trivial. Also, apologies if a similar scenario has already been answered, but I couldn't seem to find any solutions that worked for me by browsing through the similar questions.

I am trying to set up a login page for my website, which I aim to use $_SESSION variables to store the users login credentials once they have logged in. However, I can't find a way to make the $_SESSION variables persist through to other pages, or even stay when the web page has been refreshed.

It should be noted that:

Whenever I try to call session_start(), it always returns false. I call it at the very start of my php code, without any white space before it.

Whenever I call session_status, it always returns 1. This happens regardless of wherever it is called in the php code.

Whenever I call session_id, it always returns null. Again, this happens regardless of wherever it is called in the php code.

The code is being ran on SiteGround's web hosting services. I haven't tested it locally as I am relatively new to php and I'm not quite confident on how to host php files locally.

I have tested this both on; my windows PC using chrome, and my iPhone using Safari. Both have the same issues.

Any help or advice would be greatly appreciated as something which would seem rather trivial has been driving me crazy for the past couple of days.

EDIT

The following code is what I'm using to test the issues.

<?php
if (session_start())
{
    echo '<script>console.log("YES"); </script>';
} else {
    echo '<script>console.log("NO"); </script>';
}

$_SESSION["test"] = "YEET";
echo '<script>console.log("'.$_SESSION["test"].'"); </script>'; // prints YEET
?>


<?php
session_start();
if (isset($_SESSION['test']))
{
    echo '<script>console.log("'.$_SESSION["test"].'"); </script>';
} else {
    echo '<script>console.log("NEET"); </script>';
}
?>

Solution

  • Update: I found the solution.

    Even though SiteGround (my host) had set the session.save_path to be /tmp, I still had to write a custom php.ini file with this file path. This has since resolved the issue, and it was made clear that the session.save_path was the issue once I set the php.ini file to display both errors and startup errors.