Confusing session behaviour

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

    Confusing session behaviour

    I am running Apache 2, PHP 5.0.5, and windows XP

    I have one php file that populates a SESSION variable and another that
    consumes it.

    If I call the first one from the URL line of the browser, the form fills
    correctly (i.e. the values are passed in the session)

    If I call the first program from the second via an <a href> the session
    is lost.

    So, from the browser URL, the order would be:
    [first.php]
    session_start() ;
    $_SESSION['foo'] = $foo;
    session_write_c lose();
    header("locatio n: second.php");
    exit;

    [second.php]
    session_start() ;
    $foo = $_SESSION['foo'];
    print_r($foo);

    This works.

    If I change second.php to be:
    session_start() ;
    $foo = $_SESSION['foo'];
    <a href="first.php ">first</a>

    then the session is empty when the href is selected.

    What am I missing?
    -david-

  • Janwillem Borleffs

    #2
    Re: Confusing session behaviour

    David Haynes wrote:[color=blue]
    > If I call the first program from the second via an <a href> the
    > session is lost.
    >
    > So, from the browser URL, the order would be:
    > [first.php]
    > session_start() ;
    > $_SESSION['foo'] = $foo;
    >[/color]

    Where is $foo initially defined?


    JW


    Comment

    • David Haynes

      #3
      Re: Confusing session behaviour

      Janwillem Borleffs wrote:[color=blue]
      > David Haynes wrote:[color=green]
      >> If I call the first program from the second via an <a href> the
      >> session is lost.
      >>
      >> So, from the browser URL, the order would be:
      >> [first.php]
      >> session_start() ;
      >> $_SESSION['foo'] = $foo;
      >>[/color]
      >
      > Where is $foo initially defined?
      >
      >
      > JW
      >
      >[/color]
      $foo just represents some meaningful data.

      I have a real minimal example now:

      [file: foo.ctrl.php]

      <?php
      session_start() ;
      $foo[] = array('this'=>1 , 'that'=>2);
      $_SESSION['foo'] = $foo;
      session_write_c lose();
      header('locatio n: foo.php');
      exit;
      ?>

      [file: foo.php]

      <?php
      session_start() ;
      printf("_SESSIO N<br>\n");
      $foo = $_SESSION['foo'];
      print_r($foo);
      echo <<<EOF
      <html>
      <head></head>
      <body>
      <br/>
      <a href="foo.ctrl. php>foo</a>
      </body>
      EOF;
      ?>

      Calling foo.ctrl.php from the browser URL produces:
      _SESSION
      Array ( [0] => Array ( [this] => 1 [that] => 2 ) )
      foo

      clicking on the 'foo' link produces:
      _SESSION

      foo

      -david-

      Comment

      • Janwillem Borleffs

        #4
        Re: Confusing session behaviour

        David Haynes wrote:[color=blue]
        > Calling foo.ctrl.php from the browser URL produces:
        > _SESSION
        > Array ( [0] => Array ( [this] => 1 [that] => 2 ) )
        > foo
        >
        > clicking on the 'foo' link produces:
        > _SESSION
        >[/color]

        Beside that the assignment $foo[] = ... causes foo to be populated with
        another array, this works on my environment as expected.

        Make sure that the session.cookie_ path directive in your ini file is set to
        "/"; also try to set the session.use_tra ns_sid directive in the same file to
        "1", which should cause the session id to be appended to the link when there
        are problems setting the cookie.


        JW


        Comment

        • David Haynes

          #5
          Re: Confusing session behaviour

          Janwillem Borleffs wrote:[color=blue]
          > David Haynes wrote:[color=green]
          >> Calling foo.ctrl.php from the browser URL produces:
          >> _SESSION
          >> Array ( [0] => Array ( [this] => 1 [that] => 2 ) )
          >> foo
          >>
          >> clicking on the 'foo' link produces:
          >> _SESSION
          >>[/color]
          >
          > Beside that the assignment $foo[] = ... causes foo to be populated with
          > another array, this works on my environment as expected.
          >
          > Make sure that the session.cookie_ path directive in your ini file is set to
          > "/"; also try to set the session.use_tra ns_sid directive in the same file to
          > "1", which should cause the session id to be appended to the link when there
          > are problems setting the cookie.
          >
          >
          > JW
          >
          >[/color]
          Thanks for the suggestions JW.

          Unfortunately, they did not fix my problem. Maybe its a windows or php
          5.0.5. thing?

          I'll move the example to Linux and see what happens.

          -david-

          Comment

          • David Haynes

            #6
            Re: Confusing session behaviour

            David Haynes wrote:[color=blue]
            > Janwillem Borleffs wrote:[color=green]
            >> David Haynes wrote:[color=darkred]
            >>> Calling foo.ctrl.php from the browser URL produces:
            >>> _SESSION
            >>> Array ( [0] => Array ( [this] => 1 [that] => 2 ) )
            >>> foo
            >>>
            >>> clicking on the 'foo' link produces:
            >>> _SESSION
            >>>[/color]
            >>
            >> Beside that the assignment $foo[] = ... causes foo to be populated
            >> with another array, this works on my environment as expected.
            >>
            >> Make sure that the session.cookie_ path directive in your ini file is
            >> set to "/"; also try to set the session.use_tra ns_sid directive in the
            >> same file to "1", which should cause the session id to be appended to
            >> the link when there are problems setting the cookie.
            >>
            >>
            >> JW
            >>[/color]
            > Thanks for the suggestions JW.
            >
            > Unfortunately, they did not fix my problem. Maybe its a windows or php
            > 5.0.5. thing?
            >
            > I'll move the example to Linux and see what happens.
            >
            > -david-
            >[/color]
            It turns out to be firewall-related. When I disabled my (local)
            firewall, the application worked as I expected.

            Thanks for all the suggestions folks.

            -david-

            Comment

            Working...