I have struggled with this for hours but I can't get it to work. When I do a redirection to another PHP page, all my session variables are null. I am on xampp server.
session.php
<?php
session_start();
if(isset($_POST['submitted']))
{
$_SESSION['first_name'] = "MAX";
var_dump($_SESSION);
header("Location: http://localhost:8080/secure login/session2.php");
die();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859- 1" />
<title>You Logged In</title>
</head>
<body>
<form action="session.php" method="post">
<div align="center"><input type="submit" name="submit" value="Login" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
session2.php
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>You Logged In</title>
</head>
<body>
<div id="main">
<?php
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
echo 'You are welcome to session2.php <br></br>';
if (isset($_SESSION['first_name']))
{
echo $_SESSION['first_name'] . "<br></br>";
}
else
{
echo "Your session doesn't exist. I hate php <br></br>";
echo $_SESSION['first_name'];
}
?>
</div>
</body>
</html>
The session doesn't save, and the output is;
Array
(
)
You are welcome to session2.php
Your session doesn't exist. I hate php
Notice: Undefined index: first_name in C:\xampp\htdocs\secure login\session2.php on line 28
I have tried other things like changing where session variables are saved from xampp/tmp to another directory, but this didn't solve the problem. I have a program that I need to keep a user logged in when I do a redirection but this has blocked me for more than a day.
UPDATE:
The space between the directories wasn't the problem, it temporarily solved the problem but that was because there wasn't cache for the new directory yet. Any way, for a few more days, I debugged and realized that I was running two programs on my localhost. Both were using sessions, and so if one terminates the session, it also terminates the session for the other since localhost is like a domain name and there exists only one session. Particularly, the logout.php of my other program was not destroying the session but was rather jumbling it up were by you have to remove browser cache do unjumble it. I was emptying session array, destroying the session, and destroying the cookie, this was the problem and so I couldn't login again. All I had to do was just destroy the session only;
See -> Killing off Global Session Variable as a logout button
Seems like you are having problem because you have a space in name
secure login
So please try to change the name with underscore
secure_login
and also change your codeMost likely due to the
die()
call, I think that causes it not to write the session.Try
session_write_close();
prior to it.Firstly, check if there any white spaces before starting the
<?php
tag insession.php
andsession2.php
file.Than remove
var_dump($_SESSION);
fromsession.php
.and change the folder name to secure_login
Change this
In
$user = $_SESSION['first_name'] = "MAX";
first
$_SESSION['first_name'] = "MAX";
will execute, then result will be assign to$user
. so inisset($user)
it Check whether set