help with session

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stathisgotsis@hotmail.com

    help with session

    Hello everyone,
    I am having a brain fart or something, why won't this work?

    test.php:

    <?php
    session_start() ;
    $_SESSION['username']='test';
    header("Locatio n: test2.php");
    exit();
    ?>

    and test2.php:

    <?php
    session_start() ;
    echo $_SESSION['username'];
    ?>

    Any hint appreciated.

  • Erwin Moller

    #2
    Re: help with session

    stathisgotsis@h otmail.com wrote:
    [color=blue]
    > Hello everyone,
    > I am having a brain fart or something, why won't this work?
    >
    > test.php:
    >
    > <?php
    > session_start() ;
    > $_SESSION['username']='test';
    > header("Locatio n: test2.php");
    > exit();
    > ?>
    >
    > and test2.php:
    >
    > <?php
    > session_start() ;
    > echo $_SESSION['username'];
    > ?>
    >
    > Any hint appreciated.[/color]

    Hi,

    Looks fine to me.
    Is it possible PHP cannot use sessions at all because of a misconfigured
    php.ini?
    Can PHP read/write to the directory where sessions are stored (often /tmp)?
    Did you accidentally set the session_safe_ha ndler to user?

    Regards,
    Erwin Moller

    Comment

    • David Haynes

      #3
      Re: help with session

      stathisgotsis@h otmail.com wrote:[color=blue]
      > Hello everyone,
      > I am having a brain fart or something, why won't this work?
      >
      > test.php:
      >
      > <?php
      > session_start() ;
      > $_SESSION['username']='test';[/color]
      session_write_c lose(); // <-- add this[color=blue]
      > header("Locatio n: test2.php");
      > exit();
      > ?>
      >
      > and test2.php:
      >
      > <?php
      > session_start() ;
      > echo $_SESSION['username'];
      > ?>
      >
      > Any hint appreciated.
      >[/color]

      Comment

      • Erwin Moller

        #4
        Re: help with session

        David Haynes wrote:
        [color=blue]
        > stathisgotsis@h otmail.com wrote:[color=green]
        >> Hello everyone,
        >> I am having a brain fart or something, why won't this work?
        >>
        >> test.php:
        >>
        >> <?php
        >> session_start() ;
        >> $_SESSION['username']='test';[/color]
        > session_write_c lose(); // <-- add this[/color]

        Hi David,

        Why should that help?

        according to php.net:

        session_write_c lose

        (PHP 4 >= 4.0.4, PHP 5)
        session_write_c lose -- Write session data and end session
        Description
        void session_write_c lose ( void )

        End the current session and store session data.

        Session data is usually stored after your script terminated without the need
        to call session_write_c lose(), but as session data is locked to prevent
        concurrent writes only one script may operate on a session at any time.
        When using framesets together with sessions you will experience the frames
        loading one by one due to this locking. You can reduce the time needed to
        load all the frames by ending the session as soon as all changes to session
        variables are done.
        I interpret that as a possibility to quickly close the session if you know
        that you won't need it for the remainder of the script, so concurent
        request on the same session are not put in line to wait for the first
        script to finish.

        But since the OP is terminating the script test.php by calling exit(); it
        won't help. I think.
        I am missing something about session_write_c lose() maybe?

        Regards,
        Erwin Moller

        [color=blue][color=green]
        >> header("Locatio n: test2.php");
        >> exit();
        >> ?>
        >>
        >> and test2.php:
        >>
        >> <?php
        >> session_start() ;
        >> echo $_SESSION['username'];
        >> ?>
        >>
        >> Any hint appreciated.
        >>[/color][/color]

        Comment

        • David Haynes

          #5
          Re: help with session

          Erwin Moller wrote:[color=blue]
          > David Haynes wrote:
          >[color=green]
          >> stathisgotsis@h otmail.com wrote:[color=darkred]
          >>> Hello everyone,
          >>> I am having a brain fart or something, why won't this work?
          >>>
          >>> test.php:
          >>>
          >>> <?php
          >>> session_start() ;
          >>> $_SESSION['username']='test';[/color]
          >> session_write_c lose(); // <-- add this[/color]
          >
          > Hi David,
          >
          > Why should that help?
          >
          > according to php.net:
          >
          >
          > session_write_c lose
          >
          > (PHP 4 >= 4.0.4, PHP 5)
          > session_write_c lose -- Write session data and end session
          > Description
          > void session_write_c lose ( void )
          >
          > End the current session and store session data.
          >
          > Session data is usually stored after your script terminated without the need
          > to call session_write_c lose(), but as session data is locked to prevent
          > concurrent writes only one script may operate on a session at any time.
          > When using framesets together with sessions you will experience the frames
          > loading one by one due to this locking. You can reduce the time needed to
          > load all the frames by ending the session as soon as all changes to session
          > variables are done.
          >
          >
          > I interpret that as a possibility to quickly close the session if you know
          > that you won't need it for the remainder of the script, so concurent
          > request on the same session are not put in line to wait for the first
          > script to finish.
          >
          > But since the OP is terminating the script test.php by calling exit(); it
          > won't help. I think.
          > I am missing something about session_write_c lose() maybe?
          >
          > Regards,
          > Erwin Moller[/color]

          I'm a 'belt and suspenders' kind of guy when it comes to this. By using
          session_write_c lose() I know that the data has been flushed into the
          cookie prior to ending the program. It just removes one more variable in
          the mix.

          Having said that, the problem's symptoms could also be explained by
          having a setup that is blocking cookies. The session_write_c lose() is
          low hanging fruit so why not try it first?

          -david-

          Comment

          • Erwin Moller

            #6
            Re: help with session

            David Haynes wrote:
            [color=blue]
            > Erwin Moller wrote:[color=green]
            >> David Haynes wrote:
            >>[color=darkred]
            >>> stathisgotsis@h otmail.com wrote:
            >>>> Hello everyone,
            >>>> I am having a brain fart or something, why won't this work?
            >>>>
            >>>> test.php:
            >>>>
            >>>> <?php
            >>>> session_start() ;
            >>>> $_SESSION['username']='test';
            >>> session_write_c lose(); // <-- add this[/color]
            >>
            >> Hi David,
            >>
            >> Why should that help?
            >>
            >> according to php.net:
            >>
            >>
            >> session_write_c lose
            >>
            >> (PHP 4 >= 4.0.4, PHP 5)
            >> session_write_c lose -- Write session data and end session
            >> Description
            >> void session_write_c lose ( void )
            >>
            >> End the current session and store session data.
            >>
            >> Session data is usually stored after your script terminated without the
            >> need to call session_write_c lose(), but as session data is locked to
            >> prevent concurrent writes only one script may operate on a session at any
            >> time. When using framesets together with sessions you will experience the
            >> frames loading one by one due to this locking. You can reduce the time
            >> needed to load all the frames by ending the session as soon as all
            >> changes to session variables are done.
            >>
            >>
            >> I interpret that as a possibility to quickly close the session if you
            >> know that you won't need it for the remainder of the script, so concurent
            >> request on the same session are not put in line to wait for the first
            >> script to finish.
            >>
            >> But since the OP is terminating the script test.php by calling exit(); it
            >> won't help. I think.
            >> I am missing something about session_write_c lose() maybe?
            >>
            >> Regards,
            >> Erwin Moller[/color]
            >[/color]

            Hi David,
            [color=blue]
            > I'm a 'belt and suspenders' kind of guy when it comes to this. By using
            > session_write_c lose() I know that the data has been flushed into the
            > cookie prior to ending the program. It just removes one more variable in
            > the mix.
            >
            > Having said that, the problem's symptoms could also be explained by
            > having a setup that is blocking cookies. The session_write_c lose() is
            > low hanging fruit so why not try it first?[/color]

            Thanks for your reply.
            I am also a belt and suspenders kind of programmer, but this functioncall
            just doesn't make sense to me in this situation.

            But as fas as I understand the matter: It will only write the sessiondata
            away and close the session. But both actions will happen when the script
            ends.

            Of course, it is entirely possible I am missing something.

            Anyway, let's wait what the OP has to say about it.

            Regards,
            Erwin Moller
            [color=blue]
            >
            > -david-[/color]




            Comment

            • ED

              #7
              Re: help with session


              "Erwin Moller"
              <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote in
              message news:4496b875$0 $31637$e4fe514c @news.xs4all.nl ...[color=blue]
              > David Haynes wrote:
              >[color=green]
              >> Erwin Moller wrote:[color=darkred]
              >>> David Haynes wrote:
              >>>
              >>>> stathisgotsis@h otmail.com wrote:
              >>>>> Hello everyone,
              >>>>> I am having a brain fart or something, why won't this work?
              >>>>>
              >>>>> test.php:
              >>>>>
              >>>>> <?php
              >>>>> session_start() ;
              >>>>> $_SESSION['username']='test';
              >>>> session_write_c lose(); // <-- add this
              >>>
              >>> Hi David,
              >>>
              >>> Why should that help?
              >>>
              >>> according to php.net:
              >>>
              >>>
              >>> session_write_c lose
              >>>
              >>> (PHP 4 >= 4.0.4, PHP 5)
              >>> session_write_c lose -- Write session data and end session
              >>> Description
              >>> void session_write_c lose ( void )
              >>>
              >>> End the current session and store session data.
              >>>
              >>> Session data is usually stored after your script terminated without the
              >>> need to call session_write_c lose(), but as session data is locked to
              >>> prevent concurrent writes only one script may operate on a session at
              >>> any
              >>> time. When using framesets together with sessions you will experience
              >>> the
              >>> frames loading one by one due to this locking. You can reduce the time
              >>> needed to load all the frames by ending the session as soon as all
              >>> changes to session variables are done.
              >>>
              >>>
              >>> I interpret that as a possibility to quickly close the session if you
              >>> know that you won't need it for the remainder of the script, so
              >>> concurent
              >>> request on the same session are not put in line to wait for the first
              >>> script to finish.
              >>>
              >>> But since the OP is terminating the script test.php by calling exit();
              >>> it
              >>> won't help. I think.
              >>> I am missing something about session_write_c lose() maybe?
              >>>
              >>> Regards,
              >>> Erwin Moller[/color]
              >>[/color]
              >
              > Hi David,
              >[color=green]
              >> I'm a 'belt and suspenders' kind of guy when it comes to this. By using
              >> session_write_c lose() I know that the data has been flushed into the
              >> cookie prior to ending the program. It just removes one more variable in
              >> the mix.
              >>
              >> Having said that, the problem's symptoms could also be explained by
              >> having a setup that is blocking cookies. The session_write_c lose() is
              >> low hanging fruit so why not try it first?[/color]
              >
              > Thanks for your reply.
              > I am also a belt and suspenders kind of programmer, but this functioncall
              > just doesn't make sense to me in this situation.
              >
              > But as fas as I understand the matter: It will only write the sessiondata
              > away and close the session. But both actions will happen when the script
              > ends.
              >
              > Of course, it is entirely possible I am missing something.
              >
              > Anyway, let's wait what the OP has to say about it.
              >
              > Regards,
              > Erwin Moller
              >[color=green]
              >>
              >> -david-[/color]
              >
              >
              >
              >[/color]

              hi guys,

              Actually had the same issue as the op and using session_write_c lose did
              indeed solve the problem.

              The user contributed notes at:

              may shed some light here. Basically these suggest that you should call
              session_write_c lose prior to the Location header to ensure that the session
              file is correctly updated prior to the redirect (otherwise presumably, the
              session file could still be locked/being written from the previous page).

              cheers,
              ED



              Comment

              • R. Rajesh Jeba Anbiah

                #8
                Re: help with session

                Erwin Moller wrote:
                <snip>[color=blue]
                > But since the OP is terminating the script test.php by calling exit(); it
                > won't help. I think.
                > I am missing something about session_write_c lose() maybe?[/color]
                <snip>

                session_write_c lose() will be implicitly called when you use
                exit(); but as header redirection is above exit() and some time the
                session values may not be written to the desk when that page redirect
                is accessed.

                --
                <?php echo 'Just another PHP saint'; ?>
                Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                Comment

                • Erwin Moller

                  #9
                  Re: help with session

                  R. Rajesh Jeba Anbiah wrote:
                  [color=blue]
                  > Erwin Moller wrote:
                  > <snip>[color=green]
                  >> But since the OP is terminating the script test.php by calling exit(); it
                  >> won't help. I think.
                  >> I am missing something about session_write_c lose() maybe?[/color]
                  > <snip>
                  >
                  > session_write_c lose() will be implicitly called when you use
                  > exit(); but as header redirection is above exit() and some time the
                  > session values may not be written to the desk when that page redirect
                  > is accessed.[/color]

                  Hi Rajesh,

                  Aaah. Thanks, but I am still confused. :-/

                  Can i summarize it as follows?
                  When the exit() is reached, the session-writing and closing can be suspended
                  for some time (due to heavy load on the server eg.) and the next request
                  (originating from the header("Locatio n: ...") is already trying to use that
                  same session.

                  But still I do not understand what can go wrong, since as I understand it
                  the second page is waiting for the first to finish before it can access the
                  session, so it hangs and waits.
                  So no problem there as far as I can see.

                  I would love to hear more opinions because I never use
                  session_write_c lose(), but never had any sessiontroubles either, not with
                  default sessionhandling (php/file) and custom (user/database).

                  So I would love to know if it is a serious improvement, or just a myth.

                  Can you/anybody shed some light on that?

                  TIA

                  Regards,
                  Erwin Moller
                  [color=blue]
                  >
                  > --
                  > <?php echo 'Just another PHP saint'; ?>
                  > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/[/color]

                  Comment

                  • Colin McKinnon

                    #10
                    Re: help with session

                    stathisgotsis@h otmail.com wrote:
                    [color=blue]
                    > Hello everyone,
                    > I am having a brain fart or something, why won't this work?
                    >
                    > test.php:
                    >
                    > <?php
                    > session_start() ;
                    > $_SESSION['username']='test';
                    > header("Locatio n: test2.php");
                    > exit();
                    > ?>[/color]

                    Can you set a cookie in 302 response?

                    I'd look at the traffic (squid / ethereal / iehttpheaders / firefox
                    tamperData) and see. Or start the sessions before loaing test.php

                    C.

                    Comment

                    • R. Rajesh Jeba Anbiah

                      #11
                      Re: help with session

                      Erwin Moller wrote:
                      <snip>[color=blue]
                      > I would love to hear more opinions because I never use
                      > session_write_c lose(), but never had any sessiontroubles either, not with
                      > default sessionhandling (php/file) and custom (user/database).
                      >
                      > So I would love to know if it is a serious improvement, or just a myth.[/color]
                      <snip>

                      On session_start() , the page gets the session variables and on
                      exit() or implicit exit or session_write_c lose(), it writes back the
                      variables to session files--but there is a locking involved; unless any
                      of these are happened, any of the PHP files that tries to use the same
                      session/init the same session will get hanged. AFAIK, when using custom
                      session handler there is no locking involved.

                      --
                      <?php echo 'Just another PHP saint'; ?>
                      Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                      Comment

                      Working...