Excerpt from validate-login.php:
<?php
if(!session_id()){ session_start(); }
if(!(isset($_SESSION['mobile']) or isset($_SESSION['email']))){
$_SESSION['error'] = 'NOT_LOGGED_IN';
header("location:error.php");
}
?>
The script validate-login.php will be included in all the "restricted" pages which I want to be accessible only post login.
Excerpt from error.php:
<?php
if(!session_id()){ session_start(); }
$errorCode = isset($_SESSION['error']) ? $_SESSION['error'] : '';
print_r($_SESSION);
?>
Executing the code on localhost gives me output as:
Array ( [error] => NOT_LOGGED_IN )
Executing the code on remote/online server always gives me output as:
Array ( )
I HAVEN'T NOTICED THIS PROBLEM ANYWHERE ELSE IN MY WEBSITE.
If I change validate-login.php as:
<?php
if(!session_id()){ session_start(); }
if(!(isset($_SESSION['mobile']) or isset($_SESSION['email']))){
$_SESSION['error'] = 'NOT_LOGGED_IN';
print_r($_SESSION);
}
?>
The output is:
Array ( [error] => NOT_LOGGED_IN )
I have read similar questions on StackOverflow and other forums and they have suggested to remove any white spaces before session_start();
and not to output/print anything before header(location="...");
, which, I suppose, I have done that.
What is wrong with my code?