Problem with PHP session in Xampp
At it's simplest, if file_1.php contains
<?php
session_start();
$_SESSION["test_message"] = "Hello, world";
header("Location: http://localhost/file_2.php");
?>
and file_2.php con开发者_运维百科tains
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<?php
if (!(isset($_SESSION["test_message"])))
echo "Test message is not set";
else
echo $_SESSION["test_message"];
var_dump($_SESSION);
session_destroy();
?>
</body>
</html>
the result is Test message is not set
and a var_dump($_SESSION) returns null
- locally, with Xampp
. However, if I upload those same files to a paid-for hosted web site, it works and I see
Hello, world
array
'test_message' => string 'Hello, world' (length=12)
When I look at PHPinfo under Xampp it shows Session Support enabled
. What am I doing wrong?
You've forgotten the session_start at the top of file_2.php
So it should be:
<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<?php
if (!(isset($_SESSION["test_message"])))
echo "Test message is not set";
else
echo $_SESSION["test_message"];
var_dump($_SESSION);
session_destroy();
?>
</body>
</html>
session_start()
should be at the top of every file where you need to access the session functions.
EDIT:
You should really use session_write_close before redirecting to another page.
first file:
<?php
session_start();
$_SESSION["test_message"] = "Hello, world";
session_write_close();
header("Location: http://localhost/file_2.php");
?>
Session issue can be fixed in Xampp 7.1.6 do the following change in php.ini Line #1403
session.auto_start = 1
精彩评论