I am new to working with PHP sessions.
To test code using $_SESSION I have created 2 test pages(test_session.php
, get_session_test.php
). I have written this code using articles on w3schools https://www.w3schools.com/PhP/php_sessions.asp
The session variables are correctly set, I have used print_r($_SESSION)
on the test_session.php page which shows they have been set as in the code. However when I view the second page get_session_test.php no session variables are shown. I have even tried the print_r($_SESSION) and this only shows an empty array: Array ()
in test_session.php the print_r shows [favcolor] => green [favanimal] => cat
but in get_session_test.php no data shows
Here is the code on both pages:
test_session.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["favcolor"]="green";
$_SESSION["favanimal"]="cat";
echo "Session variables are set";
print_r($_SESSION);
echo '<BR><BR>';
echo $_SESSION["session_id"];
?>
</body>
</html>
get_session_test.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
echo '<BR>';
echo '<BR>';
echo 'Favourite color is'.$_SESSION["favcolor"].'<BR>';
echo 'Favourite color is'.$_SESSION["favanimal"].'<BR>';
?>
</body>
</html>