Php function run twice, why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • icui@email.it

    Php function run twice, why?

    The following script generates two files copies. (I expect that a new
    file is created in my $wrk_dir)

    The same script executed without Apache works properly and generates
    single file copy.

    With $URL_CSS_ defined works properly. (row 13)

    Deleting row 15 works properly.

    Apache seems to execute twice the script, second in background. I
    don't
    understand why.

    <?
    $id = uniqid('bug');
    $wrk_dir = ''; // Working Directory
    $src_file = 'grab.jpg'; // Source File
    $dst_file = 'tmpgrab_'.$id. '.jpg'; // New File

    echo 'Source File ='.$src_file.'< br />'; // Print Source File
    Name
    echo 'Destination File ='.$dst_file.'< br />'; // Print Destination
    File Name

    if (!copy($wrk_dir .$src_file, $wrk_dir.$dst_f ile)) {
    echo 'Failed to copy.<br />';
    }
    //$URL_CSS_='stri ng';
    ?>
    <link rel="stylesheet " href="<?=$URL_C SS_?>" type="text/css" />


    Tested with:
    Php 5.0.1 - 4.3.9 - 4.3.4
    On Linux System, kernel 2.4, 2.6

    In Windows seems to work properly. (not tested personally)


    Any ideas?
  • Pedro Graca

    #2
    Re: Php function run twice, why?

    icui@email.it wrote:[color=blue]
    > The following script generates two files copies. (I expect that a new
    > file is created in my $wrk_dir)
    >
    > The same script executed without Apache works properly and generates
    > single file copy.
    >
    > With $URL_CSS_ defined works properly. (row 13)
    >
    > Deleting row 15 works properly.
    >
    > Apache seems to execute twice the script, second in background. I
    > don't understand why.
    >
    > <?
    > $id = uniqid('bug');
    > $wrk_dir = ''; // Working Directory
    > $src_file = 'grab.jpg'; // Source File
    > $dst_file = 'tmpgrab_'.$id. '.jpg'; // New File
    >
    > echo 'Source File ='.$src_file.'< br />'; // Print Source File
    > Name
    > echo 'Destination File ='.$dst_file.'< br />'; // Print Destination
    > File Name
    >
    > if (!copy($wrk_dir .$src_file, $wrk_dir.$dst_f ile)) {
    > echo 'Failed to copy.<br />';
    > }
    > //$URL_CSS_='stri ng';
    > ?>
    > <link rel="stylesheet " href="<?=$URL_C SS_?>" type="text/css" />[/color]

    What's the HTML output of this line?
    I guess it is

    <link rel="stylesheet " href="" type="text/css" />


    I think perhaps the browser requests the "text/css" file "" (without the
    quotes) and makes Apache (correctly?) run the script twice.



    If you test $URL_CSS_ before printing the link, you should be ok:

    <?php
    unset($URL_CSS_ );
    if (rand(0, 1) == 1) $URL_CSS_ = 'string';
    ?>

    <?php if (isset($URL_CSS _)) { ?>
    <link rel="stylesheet " href="<?php echo $URL_CSS_; ?>" type="text/css" />
    <?php } ?>
    [color=blue]
    > Tested with:[/color]

    not tested! :-)

    --
    Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
    == ** ## !! !! ## ** ==
    TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
    bypass the spam filter. I will answer all pertinent mails from a valid address.

    Comment

    • i-c-u-i

      #3
      Re: Php function run twice, why?

      Pedro Graca wrote:
      [color=blue]
      > icui@email.it wrote:[color=green]
      >> The following script generates two files copies. (I expect that a new
      >> file is created in my $wrk_dir)
      >>
      >> The same script executed without Apache works properly and generates
      >> single file copy.
      >>
      >> With $URL_CSS_ defined works properly. (row 13)
      >>
      >> Deleting row 15 works properly.
      >>
      >> Apache seems to execute twice the script, second in background. I
      >> don't understand why.
      >>
      >> <?
      >> $id = uniqid('bug');
      >> $wrk_dir = ''; // Working Directory
      >> $src_file = 'grab.jpg'; // Source File
      >> $dst_file = 'tmpgrab_'.$id. '.jpg'; // New File
      >>
      >> echo 'Source File ='.$src_file.'< br />'; // Print Source File
      >> Name
      >> echo 'Destination File ='.$dst_file.'< br />'; // Print Destination
      >> File Name
      >>
      >> if (!copy($wrk_dir .$src_file, $wrk_dir.$dst_f ile)) {
      >> echo 'Failed to copy.<br />';
      >> }
      >> //$URL_CSS_='stri ng';
      >> ?>
      >> <link rel="stylesheet " href="<?=$URL_C SS_?>" type="text/css" />[/color]
      >
      > What's the HTML output of this line?
      > I guess it is
      >
      > <link rel="stylesheet " href="" type="text/css" />
      >
      >
      > I think perhaps the browser requests the "text/css" file "" (without the
      > quotes) and makes Apache (correctly?) run the script twice.
      >
      >
      >
      > If you test $URL_CSS_ before printing the link, you should be ok:
      >
      > <?php
      > unset($URL_CSS_ );
      > if (rand(0, 1) == 1) $URL_CSS_ = 'string';
      > ?>
      >
      > <?php if (isset($URL_CSS _)) { ?>
      > <link rel="stylesheet " href="<?php echo $URL_CSS_; ?>" type="text/css"
      > /> <?php } ?>
      >[color=green]
      >> Tested with:[/color]
      >
      > not tested! :-)
      >[/color]

      thanks! but I'd like to understand if it's a bug (php module? apache? or
      other...)

      other example: :)


      Run following two pages in your system and see apache log file.

      ---------------
      filename

      content
      ---------------


      ----------------------------------------
      page.htm

      <img src="" alt="No source specified" />
      ----------------------------------------


      ----------------------------------------
      page.php

      <img src="" alt="No source specified" />
      ----------------------------------------


      htm extension: page run properly, once.
      log:
      192.168.1.2 - - [17/Nov/2004:00:44:17 +0100] "GET /bug/page.htm HTTP/1.1"
      200 41


      php extension: page run twice.
      log:
      192.168.1.2 - - [17/Nov/2004:00:44:27 +0100] "GET /bug/page.php HTTP/1.1"
      200 41
      192.168.1.2 - - [17/Nov/2004:00:44:27 +0100] "GET /bug/page.php HTTP/1.1"
      200 41



      why? :)



      Comment

      • Pedro Graca

        #4
        Re: Php function run twice, why?

        i-c-u-i wrote:[color=blue]
        > Run following two pages in your system and see apache log file.
        >
        > ----------------------------------------
        > page.htm
        >
        > <img src="" alt="No source specified" />
        > ----------------------------------------
        >
        >
        > ----------------------------------------
        > page.php
        >
        > <img src="" alt="No source specified" />
        > ----------------------------------------
        >
        >
        > htm extension: page run properly, once.
        > log:
        > 192.168.1.2 - - [17/Nov/2004:00:44:17 +0100] "GET /bug/page.htm HTTP/1.1"
        > 200 41
        >
        >
        > php extension: page run twice.
        > log:
        > 192.168.1.2 - - [17/Nov/2004:00:44:27 +0100] "GET /bug/page.php HTTP/1.1"
        > 200 41
        > 192.168.1.2 - - [17/Nov/2004:00:44:27 +0100] "GET /bug/page.php HTTP/1.1"
        > 200 41
        >
        >
        >
        > why? :)[/color]

        Same thing happens here with Firefox, but not with Elinks.

        I believe the reason lies in how the browser builds the request.
        For HTML pages it uses caching, for PHP pages it doesn't.

        Unfortunately "Live HTTP headers" stopped working with Firefox and it
        would be more trouble than I care to take right now to compare all the
        request headers of the different browsers for the two pages :)
        --
        Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
        == ** ## !! !! ## ** ==
        TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
        bypass the spam filter. I will answer all pertinent mails from a valid address.

        Comment

        • Pedro Graca

          #5
          Re: Php function run twice, why?

          Pedro Graca wrote:[color=blue]
          > Same thing happens here with Firefox, but not with Elinks.[/color]

          D'oh! Of course it doesn't happen with Elinks!
          Elinks will *never* request an image.

          --
          Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
          == ** ## !! !! ## ** ==
          TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
          bypass the spam filter. I will answer all pertinent mails from a valid address.

          Comment

          • hara

            #6
            Re: Php function run twice, why?

            Pedro Graca wrote:
            [color=blue]
            > Pedro Graca wrote:[color=green]
            >> Same thing happens here with Firefox, but not with Elinks.[/color]
            >
            > D'oh! Of course it doesn't happen with Elinks!
            > Elinks will *never* request an image.
            >[/color]

            True!
            Two GET in log file.... sobh! it was easy.... :)

            Now tested with Opera and iexplorer:

            all ok

            Problem only with firefox.


            thanks for your help! ;)




            Comment

            • Martin Lucas-Smith

              #7
              Re: Php function run twice, why?



              [color=blue][color=green]
              > > <link rel="stylesheet " href="<?=$URL_C SS_?>" type="text/css" />[/color]
              >
              > What's the HTML output of this line?
              > I guess it is
              >
              > <link rel="stylesheet " href="" type="text/css" />
              >
              > I think perhaps the browser requests the "text/css" file "" (without the
              > quotes) and makes Apache (correctly?) run the script twice.[/color]

              I agree that this is probably the reason. A link to "" is generally a link
              to the same page, so Firefox is arguably correctly trying to download the
              same page as a CSS file.


              Martin
              k

              Comment

              • Michael Fesser

                #8
                Re: Php function run twice, why?

                .oO(i-c-u-i)
                [color=blue]
                >thanks! but I'd like to understand if it's a bug (php module? apache? or
                >other...)[/color]

                Neither nor.
                [color=blue]
                >----------------------------------------
                >page.htm
                >
                ><img src="" alt="No source specified" />
                >----------------------------------------
                >
                >----------------------------------------
                >page.php
                >
                ><img src="" alt="No source specified" />
                >----------------------------------------[/color]

                Results in FF 1.0:

                .... [17/Nov/2004:16:54:52 +0100] "GET /page.html HTTP/1.1" 200 109
                .... [17/Nov/2004:16:55:05 +0100] "GET /page.php HTTP/1.1" 200 123
                .... [17/Nov/2004:16:55:06 +0100] "GET /page.php HTTP/1.1" 200 123

                Results in IE 5:

                .... [17/Nov/2004:16:51:46 +0100] "GET /page.html HTTP/1.1" 200 109
                .... [17/Nov/2004:16:51:46 +0100] "GET / HTTP/1.1" 200 7959
                .... [17/Nov/2004:16:52:01 +0100] "GET /page.php HTTP/1.1" 200 123
                .... [17/Nov/2004:16:52:01 +0100] "GET / HTTP/1.1" 200 17253

                Results in Opera 7.53:

                .... [17/Nov/2004:16:39:39 +0100] "GET /page.html HTTP/1.1" 200 109
                .... [17/Nov/2004:16:39:46 +0100] "GET /page.php HTTP/1.1" 200 123
                [color=blue]
                >why? :)[/color]

                In fact all (graphical) browsers send two requests, one for the
                document, one for the image. An empty URL denotes the current document,
                so both requests are the same. The reason why in FF only one request for
                the HTML file reaches the server is that static HTML documents are
                cacheable, while PHP generated content is not cacheable by default (no
                Expires or Last-modified header).

                So the above results in FF are as I would expect, IE 5 is broken (wrong
                resolving of empty URLs) and Opera also caches the php file (I'm
                wondering why, maybe it depends on some configuration settings).

                Try it with this line:

                <img src="?" alt="No source specified">

                and you will always see two requests in your logfile, because now the
                URLs are different.

                Micha

                Comment

                • icui

                  #9
                  Re: Php function run twice, why?

                  Martin Lucas-Smith wrote:[color=blue]
                  >
                  >[color=green][color=darkred]
                  >>><link rel="stylesheet " href="<?=$URL_C SS_?>" type="text/css" />[/color]
                  >>
                  >>What's the HTML output of this line?
                  >>I guess it is
                  >>
                  >> <link rel="stylesheet " href="" type="text/css" />
                  >>
                  >>I think perhaps the browser requests the "text/css" file "" (without the
                  >>quotes) and makes Apache (correctly?) run the script twice.[/color]
                  >
                  >
                  > I agree that this is probably the reason. A link to "" is generally a link
                  > to the same page, so Firefox is arguably correctly trying to download the
                  > same page as a CSS file.
                  >
                  >
                  > Martin
                  > k[/color]

                  I think I have understood the reason of the behaviour but I think that
                  it's conceptually wrong.

                  In this case: <form action="">

                  the semplification "" = current page

                  is reasonable.

                  but it's not reasoneable that a page php with 3 null images (for a
                  stupid error :) ) runs x 3.

                  Imagine this situation:

                  A page, with 12 queries and 3 images null.
                  For this problem 12 queries become 36.
                  mulply it for number of users.... server dead :)













                  Comment

                  • Michael Fesser

                    #10
                    Re: Php function run twice, why?

                    .oO(icui)
                    [color=blue]
                    >I think I have understood the reason of the behaviour but I think that
                    >it's conceptually wrong.
                    >
                    >In this case: <form action="">
                    >
                    >the semplification "" = current page
                    >
                    >is reasonable.[/color]

                    At least that's the definition of an empty URL. Some older browsers
                    interpret it differently (current directory instead of current page).
                    [color=blue]
                    >but it's not reasoneable that a page php with 3 null images (for a
                    >stupid error :) ) runs x 3.[/color]

                    Of course it is. If you make such errors then you have to live with the
                    results. The browser just does what you tell him to do, nothing else.

                    If a document contains three URLs the browser will request them. If all
                    these URLs point to the same resource the browser will use a cached
                    version if possible. If the resource is not cacheable (like PHP-
                    generated documents by default) it sends three requests to the server.
                    That's simply how it works.
                    [color=blue]
                    >Imagine this situation:
                    >
                    >A page, with 12 queries and 3 images null.
                    >For this problem 12 queries become 36.
                    >mulply it for number of users.... server dead :)[/color]

                    I don't consider that a problem, because it won't happen on a "normal"
                    website.

                    Micha

                    Comment

                    • Tim Roberts

                      #11
                      Re: Php function run twice, why?

                      icui <icui@noemail.i t> wrote:[color=blue]
                      >
                      >I think I have understood the reason of the behaviour but I think that
                      >it's conceptually wrong.
                      >
                      >In this case: <form action="">
                      >
                      >the semplification "" = current page
                      >
                      >is reasonable.
                      >
                      >but it's not reasoneable that a page php with 3 null images (for a
                      >stupid error :) ) runs x 3.[/color]

                      That's your opinion. It happens to be wrong. Do not expect standards to
                      change because you misunderstood them.
                      [color=blue]
                      >Imagine this situation:
                      >
                      >A page, with 12 queries and 3 images null.
                      >For this problem 12 queries become 36.
                      >mulply it for number of users.... server dead :)[/color]

                      Right. Server dead because programmer stupid, or careless. You can't
                      program against stupidity.

                      What do you expect it to do? A null URL does not mean "nothing". It is a
                      shortcut for "the current page".
                      --
                      - Tim Roberts, timr@probo.com
                      Providenza & Boekelheide, Inc.

                      Comment

                      • John Dunlop

                        #12
                        Re: Php function run twice, why?

                        icui wrote:
                        [color=blue]
                        > In this case: <form action="">
                        >
                        > the semplification "" = current page[/color]

                        No; this is an exception. An empty URI reference in the
                        action attribute of a form, indeed any context where it is
                        'always intended to result in a new request', is a reference
                        to the base URI, which isn't necessarily computed from the
                        URI of the current page. 404 error pages, for example,
                        often explicitly set their base URI with the BASE element.

                        Ditch the folklore; RTFM, sec. 4.2:



                        Cheers!

                        --
                        Jock

                        Comment

                        • John Dunlop

                          #13
                          Re: Php function run twice, why?

                          Michael Fesser wrote:
                          [color=blue]
                          > .oO(i-c-u-i)[/color]
                          [color=blue][color=green]
                          > >----------------------------------------
                          > >page.htm
                          > >
                          > ><img src="" alt="No source specified" />
                          > >----------------------------------------
                          > >
                          > >----------------------------------------
                          > >page.php
                          > >
                          > ><img src="" alt="No source specified" />
                          > >----------------------------------------[/color][/color]
                          [color=blue]
                          > Results in IE 5:
                          >
                          > ... [17/Nov/2004:16:51:46 +0100] "GET /page.html HTTP/1.1" 200 109
                          > ... [17/Nov/2004:16:51:46 +0100] "GET / HTTP/1.1" 200 7959
                          > ... [17/Nov/2004:16:52:01 +0100] "GET /page.php HTTP/1.1" 200 123
                          > ... [17/Nov/2004:16:52:01 +0100] "GET / HTTP/1.1" 200 17253[/color]

                          [ ... ]
                          [color=blue]
                          > So the above results in FF are as I would expect, IE 5 is broken (wrong
                          > resolving of empty URLs) and Opera also caches the php file (I'm
                          > wondering why, maybe it depends on some configuration settings).[/color]

                          In fact IE is the only one getting it right, which makes for
                          a refreshing change.

                          --
                          Jock

                          Comment

                          Working...