Why does a session variable return to its old value?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • damezumari

    Why does a session variable return to its old value?

    I have a problem in my application I have not been able to reproduce
    completely with a simpler set of files. I know this is a crime, but
    here goes:

    In a file I have these instructions:

    echo 'before: '.$_SESSION['classtitle'].'<br />';
    require_once 'test3.php';
    echo 'after: '.$_SESSION['classtitle'].'<br />';
    echo '<p><a href="test.php" >test</a></p>';

    They display on the screen:

    before: b
    test3 before: b
    test3 after: a
    after: a

    So far, so good. But, when I click the link to test.php, this is
    displayed:

    test: b

    I don't understand how $_SESSION['classtitle'] goes back to its old
    value.

    Here are test3.php and test.php:

    test3.php:
    <?php
    session_start() ;
    echo 'test3 before: '.$_SESSION['classtitle'].'<br />';
    $_SESSION['classtitle'] = 'a';
    echo 'test3 after: '.$_SESSION['classtitle'].'<br />';
    ?>

    <?php
    session_start() ;
    echo 'test: '.$_SESSION['classtitle'].'<br />';
    ?>

    Can anyone point me in the right direction, please?

    Regards,

    Jan Nordgreen

  • damezumari

    #2
    Re: Why does a session variable return to its old value?

    I have just discovered that the problem can be simplified a bit;
    test3.php is not needed.

    $_SESSION['classtitle'] = 'b';
    echo 'main: '.$_SESSION['classtitle'].'<br />';
    unset($_SESSION['classtitle']);
    echo '<p><a href="test.php" >test</a></p>';

    This displays:

    main: b

    But when I click the link to test.php I get:

    test: a

    Conclusion: $_SESSION['classtitle'] in test.php is not the same as in
    the main program. How come?

    Regards,

    Jan Nordgreen


    Comment

    • Robin

      #3
      Re: Why does a session variable return to its old value?

      damezumari wrote:
      I have just discovered that the problem can be simplified a bit;
      test3.php is not needed.
      >
      $_SESSION['classtitle'] = 'b';
      echo 'main: '.$_SESSION['classtitle'].'<br />';
      unset($_SESSION['classtitle']);
      echo '<p><a href="test.php" >test</a></p>';
      >
      This displays:
      >
      main: b
      >
      But when I click the link to test.php I get:
      >
      test: a
      >
      Conclusion: $_SESSION['classtitle'] in test.php is not the same as in
      the main program. How come?
      >
      Regards,
      >
      Jan Nordgreen
      >
      >
      Are you calling session_start() at the top of the page that links to
      test.php?

      Robin

      Comment

      • damezumari

        #4
        Re: Why does a session variable return to its old value?

        Yes, I do.

        Comment

        • Erwin Moller

          #5
          Re: Why does a session variable return to its old value?

          damezumari wrote:
          I have just discovered that the problem can be simplified a bit;
          test3.php is not needed.
          >
          $_SESSION['classtitle'] = 'b';
          echo 'main: '.$_SESSION['classtitle'].'<br />';
          unset($_SESSION['classtitle']);
          echo '<p><a href="test.php" >test</a></p>';
          >
          This displays:
          >
          main: b
          >
          But when I click the link to test.php I get:
          >
          test: a
          >
          Conclusion: $_SESSION['classtitle'] in test.php is not the same as in
          the main program. How come?
          >
          Regards,
          >
          Jan Nordgreen
          Hi,

          This shouldn't happen.
          You unset that sessionvar, so it should be gone on test.php, and thus
          produce an error.
          You *DO* have errorreporting on I hope?
          If unsure, inspect your php.ini, or put this above all your files:
          error_reporting (E_ALL);

          If that doesn't enlighten you in any way: Why don't you post both files here
          so we can inspect them?
          (Preferably your simplified versions)

          Regards,
          Erwin Moller

          Comment

          • damezumari

            #6
            Problem solved!

            Yes, error checking is on in php.ini.

            I have found my error:

            Further down in the main program I had a new assignment to
            $_SESSION['classtitle']:

            $_SESSION['classtitle'] = 'b';
            echo 'main: '.$_SESSION['classtitle'].'<br />';
            unset($_SESSION['classtitle']);
            echo '<p><a href="test.php" >test</a></p>';
            ...
            ...
            ...
            $_SESSION['classtitle'] = a;

            It is of course the last assignment to $_SESSION['classtitle'] in the
            main program that is in effect when the user clicks the link to
            test.php, not the last assignment before the link.

            Silly me! :)

            Thanks for the responses!

            Regards,

            Jan Nordgreen

            Comment

            Working...