0

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>
  • so in both test_session which has 3 echo's and get_test_session which has 2 echo's, none of the echo's print? – Jurick Pastechi Genaro Oct 02 '17 at 12:54
  • What environment are you in? Is this on a webhost? LAN server? – GrumpyCrouton Oct 02 '17 at 12:55
  • thanks @JurickPastechiGenaro - in test_session.php the print_r shows [favcolor] => green [favanimal] => cat but in get_session_test no data shows – JC Buckingham Oct 02 '17 at 12:56
  • thanks @GrumpyCrouton - I am testing this on the internet using a basic shared hosting account with iPage – JC Buckingham Oct 02 '17 at 12:57
  • Your code is completely valid, and works on my webhost. I suspect this has to do with a configuration setting somewhere. – GrumpyCrouton Oct 02 '17 at 12:58
  • 3
    Possible duplicate of [Session data working on local but not on ipage server](https://stackoverflow.com/questions/11338747/session-data-working-on-local-but-not-on-ipage-server) – GrumpyCrouton Oct 02 '17 at 12:59
  • thanks @GrumpyCrouton ! - sorry about the duplicated post. I shall test my code on the localhost/ i have set up on my computer. I will probably now contact iPage to sort this out... – JC Buckingham Oct 02 '17 at 13:01
  • @JC Buckingham the reason i asked is because your code looks fine for me, and it works on my localhost. Are you using xamp, wamp or iis? – Jurick Pastechi Genaro Oct 02 '17 at 13:02
  • @JCBuckingham No problem at all, in fact this is one of those rare "good" duplicate posts. The wording for the question I linked you made it so you couldn't have found it easily, but now this post will likely link to the duplicate that I marked. Making more links to that post allows it to be an even better answer/question. – GrumpyCrouton Oct 02 '17 at 13:02
  • @JurickPastechiGenaro Read the other comments mate, that has already been answered. :) – GrumpyCrouton Oct 02 '17 at 13:02
  • @GrumpyCrouton after i commented i saw like 6 other comments appear, i was a bit late so my bad! :( – Jurick Pastechi Genaro Oct 02 '17 at 13:04
  • @JurickPastechiGenaro You're fine ^.^ – GrumpyCrouton Oct 02 '17 at 13:05
  • Possible duplicate of [View php session variables](https://stackoverflow.com/questions/5383270/view-php-session-variables) – Yash Kumar Verma Oct 02 '17 at 13:15

0 Answers0