Search code examples
phpsessionsession-cookiessession-variables

Checking a value exists in PHP $_SESSION fails even when the value is there


I'm trying to check for a value in a $_SESSION variable, and if the value is present redirect the user to another page. The code is as follows:

// create session
session_set_cookie_params( 0,'/',$_SERVER['HTTP_HOST'],true,true);
session_name('test');
session_start();
//  check if test session is set, if it is redirect to page-2
if (isset($_SESSION['value1']) && !empty($_SESSION['value1'])) {
    header("Location: /page-2");
    exit();
}

I know the value is there because when I go to the second page I can access it there like:

$value1 = $_SESSION['value1'];

What could I be doing wrong? Any help appreciated

Edit: The problem is isset($_SESSION['value1']) && !empty($_SESSION['value1']) seems to be returning false when I am expecting it to be true and I'm not being redirected to the second page

Session values are set on the first page after a call to an external API


Solution

  • It turns out this was an issue with load balancing and was solved by configuring sticky sessions.