I've searched through many Stack Overflow answers to this question and none seem to work for me.
I've installed XAMPP 3.2.1 and haven't touched the php.ini (well, I did, but changed it back as it didn't fix my problem).
My problem is that sessions don't seem to survive a php header redirect. I tried interrupting the script on the page the session variables are set and dumping the array -- they are being set correctly. When I do this after the redirect or even to a test page, $_SESSION is empty.
Cookies are enabled and I've checked that I have a cookie with the sessid (I do). The sessid stays the same even after redirect.
I have a feeling it has something to do with the php.ini settings or working with localhost as both I've never done before and I've always had success on using external hosting.
And yes, I have session_start before any html tags, even the doctype (it's in a header.php file that I will use on all pages.)
If you need it, here's the code when I set the variables:
$_SESSION['username'] = $user['username'];
$_SESSION['userlevel'] = $user['userlevel'];
I can do a var_dump on session and die directly after this and it displays correctly. I then do a couple of function calls and then redirect using:
header('Location: '.LANDING_PAGE);
LANDING_PAGE equates to '/index.php'. Sessions are now gone. When I cut the script just before this header and instead point to a test page to do nothing else but session_start() and var_dump the session vars, the same thing happens, no session vars -- it says:
array(0) { }
I'm stumped. Am I missing something tiny? Thanks for any help.
EDIT 1:
Here is the pertinent code:
<?php
require_once('McsBackendLibrary.php');
$indexPage = new McsBackendLibrary;
$indexPage->checkHandleErrors();
?>
<div id="mainLogo">
<img src="images/MLogo.png" />
</div>
<br />
<form method="post" action="McsUser.php">
<div class="accessClass">
Username:
<input type="text" name="username" class="accessClass" />
<br /><br />
Password:
<input type="password" name="password" class="accessClass" />
<br /><br />
<input type="submit" name="submit" class="accessClass" id="accessSub" value="Login" />
</div>
</form>
<br />
<br />
<?php require_once('footer.php'); ?>
Backend Library is:
<?php
error_reporting(E_ALL);
//McsBackendLibrary is a library of functions to be used on the webpages of MCS -- intended to separate code from design
Class McsBackendLibrary {
public function __construct() {
require_once('header.php');
}
public function checkHandleErrors() {
if (isset($_SESSION['fatalError'])) {
echo '<div id="errorBox">'.$this->session->getSessionVar('fatalError').'</div><br />';
unset($_SESSION['fatalError']);
}
}
}
?>
and header is:
<?php
require_once('McsUser.php');
session_start();
error_reporting(E_ALL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:og="http://ogp.me/ns#"
xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<title>MCS</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="McsOpenStyle.css" />
</head>
<body>
So, the index instantiates a backendlibrary object, the constructor adds the header, the header contains session_start() before any HTML tags. Here is the portion where it is redirecting:
private function fatalError() {
$_SESSION['fatalError'] = $this->errorDescr;
$this->logError();
header("Location: test.php");
/*
if ($this->errorRefer != "HOME_PAGE") {
header("Location: ".HOME_PAGE.constant($this->errorRefer));
exit();
}
header('Location: '.LANDING_PAGE);*/
exit();
}
As you can see, I interrupt the code with a header to test.php which just var_dumps $_SESSION and I get nothing. The rest is commented out but it's the code that is usually in.