0
votes

Good day.I am trying to use session variables but can not seem to get it to work. I attach my scripts.I do start the session in both the first script Test.php looks like this

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";

?>

and the second script test1.php looks like this

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>


<?php
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
                        
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body>
</html>

when i run the first one,i get the message that the variables was set.But when i run the second script,it do not show the variables.It is just blank.

i realy would appreciate any help

1
what is var_dump($_SESSION) result? - Vüsal Hüseynli
in test.php it shows Session variables are set.array(2) { ["favcolor"]=> string(5) "green" ["favanimal"]=> string(3) "cat" } - Slettie
and in test1.php NULL Favorite color is . Favorite animal is . - Slettie

1 Answers

0
votes

The php code works fine for me. It must be something in your environment settings. You may want to check .ini file settings, or browser settings to make sure that it the browser is storing and sending the session id cookie with each request. Without that cookie, the server won't know which session to start.

Try running a file with phpinfo() and search through all the settings under session. That might point you in the direction of a .ini file issue. You can also open your browser dev tools and look at the application tab for your site. There should be a cookie stored for your site that correlates to the session name, probably PHPSESSID.