(I couldn't think of a better title)
I have a login page. When a user logs in successfully their userid is written to a session variable.
The user then switches to another page, at which point my script tells them they're not logged in (i.e. the session variable doesn't exist).
But, if I put print_r($_SESSION); in my code it appears to force the session variable to 'exist'. I can't figure this out - very similar code works just fine on another site of mine.
Relevant code:
Login page (works as expected):
session_start();
include("includes/login_functions.php");
require("../../localrefs/slideshow/adminconnect.php");
if ($_POST['userid'] && $_POST['password']) {
$result = mysql_query("SELECT * FROM users WHERE name='" . $_POST['userid'] . "' and pass=password('" . $_POST['password'] . "')", $db);
if (mysql_num_rows($result) >0) {
$userrow = mysql_fetch_array($result);
$_SESSION['this_user'] = $_POST['userid'];
$_SESSION['this_realname'] = $userrow['realname'];
}
}
if ($_SESSION['this_user']) {
include("includes/adminhead.php");
echo "logged in as " . $_SESSION['this_user'];
echo "<a href=\"index.php?logout=true\">logout</a>";
} else {
include("includes/loginform.php");
}
Second page:
session_start();
include("includes/adminhead.php");
require("../../localrefs/slideshow/adminconnect.php");
if ($_SESSION['this_user']) {
// do some stuff
} else {
echo "Not logged in";
}
now if I just add the print_r like this, it works:
session_start();
print_r($_SESSION);
include("includes/adminhead.php");
require("../../localrefs/slideshow/adminconnect.php");
if ($_SESSION['this_user']) {
// do some stuff
} else {
echo "Not logged in";
}
I should note that I seem to be having my problems in Mac OS X Safari 1.01, not in Explorer 5.2.2. There must be some slop in my code somewhere that's causing this, as I've had no problems with my other site with very similar code.
Any suggestions?