My script, test.php
, is below. When I click on Submit, it processes this as a login attempt and successfully sets up $_SESSION
variables. But when I reload the page (by clicking a link back to itself), $_SESSION
is empty.
<?php
//test.php
session_start ();
function isUserLoggedIn (&$username)
{
$loggedIn = isset ($_SESSION['loggedin']);
if ($loggedIn)
$username = $_SESSION['user'];
else
$username = '';
return $loggedIn;
}
function processLogin ()
{
$_SESSION['loggedin'] = TRUE;
$_SESSION['user'] = "podunk";
session_write_close ();
}
echo '$_SESSION before we do anything..................: ';
print_r ($_SESSION);
echo "<br>";
if (isset($_POST['Submit'])) processLogin ();
$loggedIn = isUserLoggedIn ($username);
echo '$_SESSION after processing any login attempt: ';
print_r ($_SESSION);
echo "<br>";
if ($loggedIn)
echo "I AM LOGGED IN as $username!";
else
echo "I am logged out :(";
?>
<html>
<body>
<form name="form1" method="post" action="test.php">
<input type="submit" name="Submit" value="Login">
</form>
<a href="test.php">Reload page</a>
</body>
</html>
This is when I run it on my Linux apache2 server. When I run it on XAMPP, $_SESSION persists and the user remains logged in. So I am guessing it's something to do with php.ini... but maybe I've got a mistake here and XAMPP is being forgiving.
The Linux server is successfully running WordPress, so its setup can't be too strange. Just did a system upgrade, as recommended. The session.save_path exists and has something in it dated today, so I assume it's working, although it's owned by root not www-data, so IDK if that's an issue.
Here are some other things from php info. IDK about that cookie_path, so I changed it to the same place as session.save_path, FWIW.
session.auto_start On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
Behavior is the same using Chrome, Firefox, and IE.
TIA.