"Using PHP session variables on second page"
P粉186904731
2023-09-03 11:31:53
<p>Hello. I'm trying to use session variables but can't seem to get it to work.
I've attached my script. I started the session in both scripts.
The first script Test.php looks like this</p>
<pre class="brush:php;toolbar:false;"><?php
// Start session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
//Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variable set. ";
?></pre>
<p>The second script test1.php looks like this</p>
<pre class="brush:php;toolbar:false;"><?php
// Start session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo "My favorite color is" . $_SESSION["favcolor"] . ".<br>";
echo "My favorite animal is" . $_SESSION["favanimal"] . ". ";
?>
</body>
</html></pre>
<p>When I run the first script I get the message that the variable has been set. But when I run the second script it doesn't show the variables. It's just blank. </p>
<p>I would really appreciate any help</p>
This PHP code works fine for me. There may be something wrong with your environment settings. You can check the settings in your
.ini
file, or your browser settings, to ensure that the browser stores and sends the session ID cookie on every request. Without this cookie, the server does not know which session to start.Try running a file with
phpinfo()
and search for all settings undersession
. This may point to a problem with the .ini file. You can also open your browser's developer tools and view your site's Applications tab. There should be a cookie stored on your site associated with the session name, probablyPHPSESSID
.