Session not being stored
I know that this is a duplicate of 2/3 other questions, but I tried the solutions proposed, and they did not work for me. I suspect the problem is in the PHP.ini file, or with the server folder permissions.
Login page:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
//EDIT: forgot to copy paste this, but it is in my code
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) {
//login function basically checks the posted username and password, then sets some
//session variables accordingly
login($_POST['username'], $_POST['password']);
}
if (isset($_SESSION['valid']) && $_SESSION['valid'] === true) {
header("Location: /HomePage/Home.php");
exit;
}
...
?>
Home page:
<?php
//EDIT: forgot to copy paste this, but it is in my code
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (!isset($_SESSION['valid']) || $_SESSION['valid'] !== true) {
header("Location: /index.php");
exit;
}
...
?>
Now here's the weird part. According to firebug, when I enter a valid username and password, I get redirected to the home page (as predicted) and then I get redirected back to the login page. I do a var_dump of session on the home page to check, and session is empty.
EDIT 2: When I replace the header with
echo "Session is " . session_id() . "<br />";
on the login page, the session_id is blank. Is that the proble开发者_如何学编程m? How do I fix it?
EDIT: It was a problem with the PHP ini file, thanks for the help.
I don't see any session_start()
calls in either PHP script. You won't have a session until you call that function (and it must be called on every page that uses sessions).
You have to call session_start();
or configure session.auto_start
in your php.ini
精彩评论