1

Why is my $_SESSION data not being preserved across pages? I can access $_SESSION data over and over on the same page, but not from any page other than the one it was set on.

Index.php (Set attempt):

<?php
    session_start();
    $_SESSION['TEST'] = '1';
?>
<!doctype html>
<html lang="en">
    <head>
        <title>Title</title>
    </head>
    <body>
        <h1>Heading</h1>
        <a href="PageTWO.php">Page TWO</a>
    </body>
</html>

PageTWO.php (Get attempt):

<?php
    session_start();
?>
<!doctype html>
<html lang="en-us">
    <head>
        <title>Title</title>
    </head>
    <body>
        <?
            echo $_SESSION['TEST'];
        ?>
    </body>
</html>
SE13013
  • 343
  • 3
  • 14
  • Is this all the code you have, or didn't you post everything? – Daan Jun 25 '15 at 09:43
  • This is all the code I have. I've only _just_ started writing code so that's all there is right now. – SE13013 Jun 25 '15 at 09:46
  • Then it's probably a setting in php.ini or a misconfiguration of your server. – Daan Jun 25 '15 at 09:48
  • This has nothing to do with the fact that's opensource. How did you configure your server what changes did you made to your php.ini file? – Daan Jun 25 '15 at 09:52
  • Change your php tag from `` to ` – Sariban D'Cl Jun 25 '15 at 09:55
  • @SE13013 Have a look at the top answer here: http://stackoverflow.com/questions/17242346/php-session-lost-after-redirect – Daan Jun 25 '15 at 09:56
  • Thank you @Daan I will sew how it goes! – SE13013 Jun 25 '15 at 10:00
  • echo $_SESSION['TEST'] in index.php page, check if it prints something or not, if not then that means session on your server is not working – Amrinder Singh Jun 25 '15 at 10:31
  • Sorry if anyone was waiting for a response, I fell asleep. @Insomania I can confirm that $_SESSION does work on the same page, but as soon as I go to another page, it is lost. – SE13013 Jun 26 '15 at 00:51
  • @Daan In the question you linked to, the top answer states that `register_globals` must be turned off. Upon looking (and Ctrl+F), there is no `register_globals` in my PHP.ini file and no `register_globals` listed in phpinfo() output either. I added a line `register_globals=Off` to the PHP.ini file (I think that's how you add it) but still no change in behavior (re $_SESSION). I'm continuing on with the link's other suggestions. – SE13013 Jun 26 '15 at 00:59
  • @Daan I've just gone through all the suggestions, and I still can't get $_SESSION to work. Do you have any other ideas? – SE13013 Jun 26 '15 at 01:15
  • I've confirmed that the session save path is C:\WINDOWS\temp\ and that the server has rights to write to that file/location. Echoeing session data _on the page it was created_ works but echoeing that same data on a page _other than the one it was created on_ does not work. register_globals is OFF. I've tried calling session_start() on index.php. I've also tried calling it on every other page too (at the very top of the file). Makes no difference. I have tried everything I have come across online. It seems as though there are about 1 trillion ways for sessions to not work. I WANT MY NEEDLE! :( – SE13013 Jun 26 '15 at 02:40

1 Answers1

-1

Problem is in your PHP tag.

Your tag should be

<?php
   echo $_SESSION['TEST'];
?>
Sariban D'Cl
  • 2,197
  • 2
  • 28
  • 42