Baffling! Redirects with header() takes minutes to respond.

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

    #1

    Baffling! Redirects with header() takes minutes to respond.

    Something odd is happening. Scripts on several sites that collect form
    data, save it to a DB, then redirect the user to another page are
    slowing to a crawl during the redirect using the header() function. For
    example: header("Locatio n:member.php").

    I tried changing the header() command in a few ways:
    header("Locatio n:/member.php"), header("Locatio n:./member.php"),
    header("Locatio n:http://www.site.com/member.php"). But none of these
    made any difference.

    I then discovered that if I put "exit;" after the header() function,
    there's no more delay and the redirect is instantaneous again.

    What's odd is that these pages worked just fine without the exit
    command until recently. I've made no changes to those scripts, or to
    PHP, so, I can't figure out why this all of a sudden started happening.

    Has anyone else noticed this? Is putting an "exit;" right after a
    header() now required in PHP? Because I never had to do this before and
    it always worked fine.

    Using PHP 4.3.2 on Linux Red Hat ES 3.0 server. Tested this in Safari,
    Firefox and MS IE, with the same (slow) redirect results (until "exit;"
    was added).

    Monty

  • Andy Hassall

    #2
    Re: Baffling! Redirects with header() takes minutes to respond.

    On 15 Nov 2005 14:02:36 -0800, "Monty" <monty3@hotmail .com> wrote:
    [color=blue]
    >Something odd is happening. Scripts on several sites that collect form
    >data, save it to a DB, then redirect the user to another page are
    >slowing to a crawl during the redirect using the header() function. For
    >example: header("Locatio n:member.php").[/color]

    The HTTP standards prohibit relative URIs in the Location header, so you're
    relying on whatever the browser does to compensate for invalid headers.

    http://www.w3.org/Protocols/rfc2616/....html#sec14.30
    [color=blue]
    >I tried changing the header() command in a few ways:
    >header("Locati on:/member.php"), header("Locatio n:./member.php"),
    >header("Locati on:http://www.site.com/member.php"). But none of these
    >made any difference.[/color]

    The final one is at least valid.
    [color=blue]
    >I then discovered that if I put "exit;" after the header() function,
    >there's no more delay and the redirect is instantaneous again.
    >
    >What's odd is that these pages worked just fine without the exit
    >command until recently. I've made no changes to those scripts, or to
    >PHP, so, I can't figure out why this all of a sudden started happening.
    >
    >Has anyone else noticed this? Is putting an "exit;" right after a
    >header() now required in PHP? Because I never had to do this before and
    >it always worked fine.[/color]

    It's never been required, but for Location it makes sense; there's not a great
    deal of point outputting more content after it.

    What code actually runs after the header? Odds are something has changed
    there. You mention a database; has a table got considerably larger over time,
    resulting in a previously fast query turning into a long-running query running
    after the header() call?
    [color=blue]
    >Using PHP 4.3.2 on Linux Red Hat ES 3.0 server. Tested this in Safari,
    >Firefox and MS IE, with the same (slow) redirect results (until "exit;"
    >was added).[/color]

    4.3.2 is rather old - 4.3.11 is the latest on that branch, or 4.4.1 for the
    latest PHP4. Probably not immediately relevant, but there are a lot of nasty
    bugs if you're that far behind, including security issues.
    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Monty

      #3
      Re: Baffling! Redirects with header() takes minutes to respond.

      The PHP version on my server is part of the standard Red Hat Linux ES
      3.0 bundle, and is updated by RedHat, so, while the version may be
      4.3.2, it has all the fixes and security updates already applied by
      RedHat Up2date.

      None of the things you mention as a possible cause for this are
      relevant, actually, because the fix is to add an "exit;" after a
      header. Once I do that, it works as it used to. If the cause was
      something else, I presume adding an exit would make no difference, but
      it does. As I mentioned previously, I have made no other changes to
      this code or PHP before this issue arose, the database table is not
      large at all, and even when using a full, non-relative domain in the
      header() function, it made no difference at all.

      The only thing that did work was to add "exit;" after all
      header("Locatio n:...") functions. I checked the online PHP manual, and
      there's no mention that this is necessary, so, I am totally baffled as
      to why this suddenly started happening.

      Comment

      • Wayne

        #4
        Re: Baffling! Redirects with header() takes minutes to respond.

        On 15 Nov 2005 18:35:06 -0800, "Monty" <monty3@hotmail .com> wrote:
        [color=blue]
        >The only thing that did work was to add "exit;" after all
        >header("Locati on:...") functions. I checked the online PHP manual, and
        >there's no mention that this is necessary, so, I am totally baffled as
        >to why this suddenly started happening.[/color]

        The header("Locatio n:...") function does not terminate your script.
        So any processing after the header() will continue. Usually that's
        not what you want -- you usually want to exit() at that point.

        What does you script do after the header is sent? Chances are, it's
        still doing something.

        Comment

        • Peter Fox

          #5
          Re: Baffling! Redirects with header() takes minutes to respond.

          Following on from Monty's message. . .[color=blue]
          >The only thing that did work was to add "exit;" after all
          >header("Locati on:...") functions. I checked the online PHP manual, and
          >there's no mention that this is necessary, so, I am totally baffled as
          >to why this suddenly started happening.
          >[/color]
          Just out of curiosity. What happens if you replace exit; with
          print("after header"); ? (I can't say I've ever had this prob 'cos I
          always used exit anyway.)



          --
          PETER FOX Not the same since the bookshop idea was shelved
          peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
          2 Tees Close, Witham, Essex.
          Gravity beer in Essex <http://www.eminent.dem on.co.uk>

          Comment

          • Wayne

            #6
            Re: Baffling! Redirects with header() takes minutes to respond.

            On Wed, 16 Nov 2005 10:02:04 +0000, Peter Fox
            <peterfox@emine nt.demon.co.uk. not.this.bit.no .html> wrote:
            [color=blue]
            >Following on from Monty's message. . .[color=green]
            >>The only thing that did work was to add "exit;" after all
            >>header("Locat ion:...") functions. I checked the online PHP manual, and
            >>there's no mention that this is necessary, so, I am totally baffled as
            >>to why this suddenly started happening.
            >>[/color]
            >Just out of curiosity. What happens if you replace exit; with
            >print("after header"); ? (I can't say I've ever had this prob 'cos I
            >always used exit anyway.)[/color]

            You shouldn't see anything. You'll end up transmitting "after header"
            to the browser but it'll see the location header and redirect anyway.

            Comment

            • Andy Hassall

              #7
              Re: Baffling! Redirects with header() takes minutes to respond.

              On 15 Nov 2005 18:35:06 -0800, "Monty" <monty3@hotmail .com> wrote:
              [color=blue]
              >The PHP version on my server is part of the standard Red Hat Linux ES
              >3.0 bundle, and is updated by RedHat, so, while the version may be
              >4.3.2, it has all the fixes and security updates already applied by
              >RedHat Up2date.[/color]

              Ah. Fair enough.
              [color=blue]
              >None of the things you mention as a possible cause for this are
              >relevant, actually, because the fix is to add an "exit;" after a
              >header. Once I do that, it works as it used to. If the cause was
              >something else, I presume adding an exit would make no difference, but
              >it does.[/color]

              I'm suggesting that some of your code that is running after the header() call
              that is taking a significant time.

              Adding exit immediately after header() prevents that code running.

              For a trivial example:

              <?php
              header("Locatio n: http://www.php.net/");
              sleep(10);
              ?>

              This takes 10 seconds to actually redirect. So the code that runs after
              header() is significant. exit skips it. If it worked before, then surely it is
              a possible cause that the code used to run quickly so you didn't notice, but
              now it runs slowly.

              So, what is running after the header call?
              --
              Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
              http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

              Comment

              • Monty

                #8
                Re: Baffling! Redirects with header() takes minutes to respond.

                > So, what is running after the header call?

                Hi. The only code that would be executed after the header() call is to
                load a record from the DB to display it in the form. That's it. When I
                remove the header() call completely (allowing the page to reload), it
                happens instantly, so, I don't think the code that follows is causing
                the delay. But, if I put the header() call back in without an exit, it
                takes hangs for minutes before moving forward.

                And the odd thing is that this never happened before, and I didn't make
                any changes at all to these scripts that are all of sudden hanging at a
                header() call if not followed by exit. But, it's also not ALL header()
                calls doing this. On some pages without an exit, it happens just as
                fast as when I add the exit, so, the results seem to be very
                inconsistent.

                I guess no one else has been having this problem. Very strange!

                Comment

                Working...