PHP: Session/redirect puzzle

Discussion in 'NZ Computing' started by E.C., Nov 11, 2003.

  1. E.C.

    E.C. Guest

    Re: Session/redirect puzzle

    "nick" <> wrote in message
    news:...
    > Hi PHP experts out there,
    >
    > Please help me to solve this puzzle:
    >
    > What happens is when a.php is run, which redirects to b.php, b.php
    > shows nothing (should print "bla..."). It appears that doit() is run
    > in a.php which clears $_SESSION['msg'] resulting in b.php getting a
    > blank $_SESSION['msg'] (if I take out $_SESSION['msg'] = "" from the
    > doit() function b.php shows "bla..."). But how can that be?? doit()
    > should never run in a.php due to the redirect, right?? I'm working on
    > a unix server.


    It will keep going, as you might want to print out some HTML for clients
    that don't see the redirect. i.e. a manual meta tag to refresh the page. So
    in other words, call exit after the redirect.
     
    E.C., Nov 11, 2003
    #1
    1. Advertising

  2. E.C.

    madknoxie Guest

    In article <>,
    nick <> wrote:

    > But how can that be?? doit()
    > should never run in a.php due to the redirect, right??


    You may be wrong, think of the flow of instructions. If I read it
    correctly (starting with a.php), you are requiring c.php which in turn
    executes the function doit() which explains all the behavior you have
    described. The redirect is after including c.php. Just out of curiosity,
    what are you trying to achieve with these three files?

    --
    madknoxie
    $35 .nz domain names: http://www.ivision.co.nz/
     
    madknoxie, Nov 11, 2003
    #2
    1. Advertising

  3. E.C.

    madknoxie Guest

    In article <>,
    madknoxie <> wrote:

    > In article <>,
    > nick <> wrote:
    >
    > > But how can that be?? doit()
    > > should never run in a.php due to the redirect, right??

    >
    > You may be wrong, think of the flow of instructions. If I read it
    > correctly (starting with a.php), you are requiring c.php which in turn
    > executes the function doit() which explains all the behavior you have
    > described. The redirect is after including c.php. Just out of curiosity,
    > what are you trying to achieve with these three files?


    What version of PHP are you using?

    --
    madknoxie
    $35 .nz domain names: http://www.ivision.co.nz/
     
    madknoxie, Nov 11, 2003
    #3
  4. E.C.

    Gurble Guest

    On Wed, 12 Nov 2003 00:17:19 +1300, madknoxie
    <> wrote:

    >In article <>,
    > nick <> wrote:
    >
    >> But how can that be?? doit()
    >> should never run in a.php due to the redirect, right??

    >
    >You may be wrong, think of the flow of instructions. If I read it
    >correctly (starting with a.php), you are requiring c.php which in turn
    >executes the function doit() which explains all the behavior you have
    >described. The redirect is after including c.php. Just out of curiosity,
    >what are you trying to achieve with these three files?


    Without having much of a look at it, I don't think that's correct.
    He's including c.php, which should include the function definition,
    but not actually execute it.

    Adding a little extra proves that the result we see from doit() is
    indeed run from b.php. (the code below returns 'b', meaning that it
    the output shown is from b.php.)

    Note, of course, you don't need session_start in c.php - otherwise
    you're essentially performing it twice when you require c.php (why?).

    Your unexplained error is because when using a redirect header
    (location:), there is no guarantee that additional code is not run
    until the browser picks up the redirect (this is a call to the browser
    to redirect, rather than *necessarily* redirecting immediately). This
    is fixed by adding an exit in a.php, after the header redirect, to
    ensure no additional code is executed.

    So, the following should work (not tested). Let us know how it goes.

    ************ a.php ************
    session_start();
    require("c.php");

    if (true) {
    $msg = "bla...";
    session_register("msg");
    header("location: b.php");
    exit;
    }

    doit('a');

    ************ b.php ************
    <?
    session_start();
    require("c.php");
    doit('b');
    ?>

    ************ c.php ************
    <?
    function doit($from) {
    print "From $from - " . $_SESSION['msg'];
    $_SESSION['msg'] = "";
    }
    ?>
     
    Gurble, Nov 11, 2003
    #4
  5. E.C.

    dOTdASH Guest

    Re: Session/redirect puzzle

    "nick" <> wrote in message
    news:...
    > Hi PHP experts out there,
    >
    > Please help me to solve this puzzle:
    >
    > What happens is when a.php is run, which redirects to b.php, b.php
    > shows nothing (should print "bla..."). It appears that doit() is run
    > in a.php which clears $_SESSION['msg'] resulting in b.php getting a
    > blank $_SESSION['msg'] (if I take out $_SESSION['msg'] = "" from the
    > doit() function b.php shows "bla..."). But how can that be?? doit()
    > should never run in a.php due to the redirect, right?? I'm working on
    > a unix server.
    >
    > I'm deeply troubled by this and if someone could shed some light on
    > this I would truly appreciate it.
    >
    > ************ a.php ************
    > session_start();
    > require("c.php");
    >
    > if (true) {
    > $msg = "bla...";
    > session_register("msg");
    > header("location: b.php");
    > }
    >
    > doit();
    >
    > ************ b.php ************
    > <?
    > session_start();
    > require("c.php");
    > doit();
    > ?>
    >
    > ************ c.php ************
    > <?
    > session_start();
    > function doit() {
    > print $_SESSION['msg'];
    > $_SESSION['msg'] = "";
    > }
    > ?>


    dunno anything about php but I do know how to set a system clock :)
     
    dOTdASH, Nov 12, 2003
    #5
  6. E.C.

    Gurble Guest

    On Sat, 15 Nov 2003 17:13:41 +1300, nick <> wrote:

    >You're the man! Though I have to say I still don't understand this coz
    >the same code behaves differently under Unix than Windows (the code
    >works as expected under Windows).


    Actually, it sort-of works as expected under Linux, not Windows.

    If you think of the fact that PHP is a Pre-Processor, and that the
    Header command is a command sent to the browser, PHP does something
    like this:

    Start Session.
    Add definition of doit from c.php.
    Set $msg variable.
    Make it a session variable.
    Output Header command.
    *1
    Call doit.
    Output $msg session variable.
    Clear $msg session variable.
    Finish
    *2

    Then, at some point between *1 and *2, the browser will pop on off and
    open b.php.

    Now, considering the speed at which PHP executes (compared with data
    transfer between the server and client - in this case the browser), I
    would have expected it to finish (get to *2) *before* the browser had
    received the Header command, thought about it, then redirected to
    b.php.

    I actually find it quite strange that when the server is running under
    Windows, it doesn't have time to get to *2 before being redirected - I
    would have thought the process would finish regardless.

    Was the Windows server the local machine by any chance? What web
    server are you using? I do know that the performance of Apache under
    Linux generally beats Apache under Windows, and definately IIS, so
    maybe that's the issue here.

    >Thanks alot for the help. Learn something everyday.


    No problem at all. My mum used to always say that a day without
    learning something is a day wasted... I don't think she ever saw the
    irony of the statement... ;-)
     
    Gurble, Nov 13, 2003
    #6
  7. E.C.

    nick Guest

    Hi PHP experts out there,

    Please help me to solve this puzzle:

    What happens is when a.php is run, which redirects to b.php, b.php
    shows nothing (should print "bla..."). It appears that doit() is run
    in a.php which clears $_SESSION['msg'] resulting in b.php getting a
    blank $_SESSION['msg'] (if I take out $_SESSION['msg'] = "" from the
    doit() function b.php shows "bla..."). But how can that be?? doit()
    should never run in a.php due to the redirect, right?? I'm working on
    a unix server.

    I'm deeply troubled by this and if someone could shed some light on
    this I would truly appreciate it.

    ************ a.php ************
    session_start();
    require("c.php");

    if (true) {
    $msg = "bla...";
    session_register("msg");
    header("location: b.php");
    }

    doit();

    ************ b.php ************
    <?
    session_start();
    require("c.php");
    doit();
    ?>

    ************ c.php ************
    <?
    session_start();
    function doit() {
    print $_SESSION['msg'];
    $_SESSION['msg'] = "";
    }
    ?>
     
    nick, Nov 14, 2003
    #7
  8. E.C.

    nick Guest

    On Wed, 12 Nov 2003 08:15:50 +1300, Gurble <> wrote:

    >On Wed, 12 Nov 2003 00:17:19 +1300, madknoxie
    ><> wrote:
    >
    >>In article <>,
    >> nick <> wrote:
    >>
    >>> But how can that be?? doit()
    >>> should never run in a.php due to the redirect, right??

    >>
    >>You may be wrong, think of the flow of instructions. If I read it
    >>correctly (starting with a.php), you are requiring c.php which in turn
    >>executes the function doit() which explains all the behavior you have
    >>described. The redirect is after including c.php. Just out of curiosity,
    >>what are you trying to achieve with these three files?

    >
    >Without having much of a look at it, I don't think that's correct.
    >He's including c.php, which should include the function definition,
    >but not actually execute it.
    >
    >Adding a little extra proves that the result we see from doit() is
    >indeed run from b.php. (the code below returns 'b', meaning that it
    >the output shown is from b.php.)
    >
    >Note, of course, you don't need session_start in c.php - otherwise
    >you're essentially performing it twice when you require c.php (why?).
    >
    >Your unexplained error is because when using a redirect header
    >(location:), there is no guarantee that additional code is not run
    >until the browser picks up the redirect (this is a call to the browser
    >to redirect, rather than *necessarily* redirecting immediately). This
    >is fixed by adding an exit in a.php, after the header redirect, to
    >ensure no additional code is executed.
    >


    You're the man! Though I have to say I still don't understand this coz
    the same code behaves differently under Unix than Windows (the code
    works as expected under Windows).

    Thanks alot for the help. Learn something everyday.

    Nick.
     
    nick, Nov 15, 2003
    #8
  9. E.C.

    nick Guest

    On Wed, 12 Nov 2003 00:25:39 +1300, madknoxie
    <> wrote:

    >In article <>,
    > madknoxie <> wrote:
    >
    >> In article <>,
    >> nick <> wrote:
    >>
    >> > But how can that be?? doit()
    >> > should never run in a.php due to the redirect, right??

    >>
    >> You may be wrong, think of the flow of instructions. If I read it
    >> correctly (starting with a.php), you are requiring c.php which in turn
    >> executes the function doit() which explains all the behavior you have
    >> described. The redirect is after including c.php. Just out of curiosity,
    >> what are you trying to achieve with these three files?

    >
    >What version of PHP are you using?


    4.3.3. Problem explained (sortof) by Gurble though still puzzled by
    the fact that PHP, under this incidence, is better implemented under
    Windows than under Unix (see my reply to Gurble). Luckily I've used a
    redirect() function throughout my site otherwise I'll have to go
    through each file, look for "header('location:.....')" and append
    "exit" under each, which would be a total PITS.

    Thanks guys for your input.

    Nick.

    P/S: Got any contract you'd like an extra hand on?
     
    nick, Nov 15, 2003
    #9
  10. E.C.

    nick Guest

    On Thu, 13 Nov 2003 19:54:27 +1300, Gurble <> wrote:

    >On Sat, 15 Nov 2003 17:13:41 +1300, nick <> wrote:
    >
    >>You're the man! Though I have to say I still don't understand this coz
    >>the same code behaves differently under Unix than Windows (the code
    >>works as expected under Windows).

    >
    >Actually, it sort-of works as expected under Linux, not Windows.
    >
    >If you think of the fact that PHP is a Pre-Processor, and that the
    >Header command is a command sent to the browser, PHP does something
    >like this:
    >
    >Start Session.
    >Add definition of doit from c.php.
    >Set $msg variable.
    >Make it a session variable.
    >Output Header command.
    >*1
    >Call doit.
    >Output $msg session variable.
    >Clear $msg session variable.
    >Finish
    >*2


    No believe me I completely understand this (though not before you
    tipped me). What I meant by "work as expected" is that a "redirect"
    *should* take you to another page straight away, whether it's from the
    client's or server's perspective. That is simply how it *should*
    function. With PHP (on Apache/Unix anyway), we have to do that extra
    step (exit()), which could be a complete waste of time to debug for
    newbies like me. I guess I've always treated the header/location
    command like response.redirect in ASP, which was a huge mistake.

    >Was the Windows server the local machine by any chance? What web
    >server are you using? I do know that the performance of Apache under
    >Linux generally beats Apache under Windows, and definately IIS, so
    >maybe that's the issue here.


    Come to think of it I've never tried the exact same code under
    Windows, just the impression that Windows never gave me redirect
    problem. I might test this code with Windows if I got the chance. The
    Windows server is a remote server.
     
    nick, Nov 17, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sk

    A puzzle to puzzle you

    sk, Jul 17, 2004, in forum: A+ Certification
    Replies:
    1
    Views:
    619
    Geoff
    Jul 17, 2004
  2. raviraj joshi
    Replies:
    0
    Views:
    674
    raviraj joshi
    Jul 4, 2009
  3. EVS
    Replies:
    0
    Views:
    1,984
  4. infocus
    Replies:
    0
    Views:
    909
    infocus
    Jul 19, 2010
  5. EVS
    Replies:
    0
    Views:
    1,852
Loading...

Share This Page