473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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_file)) {
echo 'Failed to copy.<br />';
}
//$URL_CSS_='string';
?>
<link rel="stylesheet" href="<?=$URL_CSS_?>" 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?
Jul 17 '05 #1
12 8848
ic**@email.it wrote:
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_file)) {
echo 'Failed to copy.<br />';
}
//$URL_CSS_='string';
?>
<link rel="stylesheet" href="<?=$URL_CSS_?>" type="text/css" />
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 } ?>
Tested with:


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.
Jul 17 '05 #2
Pedro Graca wrote:
ic**@email.it wrote:
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_file)) {
echo 'Failed to copy.<br />';
}
//$URL_CSS_='string';
?>
<link rel="stylesheet" href="<?=$URL_CSS_?>" type="text/css" />


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 } ?>
Tested with:


not tested! :-)


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? :)

Jul 17 '05 #3
i-c-u-i wrote:
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? :)


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.
Jul 17 '05 #4
Pedro Graca wrote:
Same thing happens here with Firefox, but not with Elinks.


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.
Jul 17 '05 #5
Pedro Graca wrote:
Pedro Graca wrote:
Same thing happens here with Firefox, but not with Elinks.


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


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! ;)


Jul 17 '05 #6


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


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.


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
Jul 17 '05 #7
.oO(i-c-u-i)
thanks! but I'd like to understand if it's a bug (php module? apache? or
other...)
Neither nor.
----------------------------------------
page.htm

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

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

<img src="" alt="No source specified" />
----------------------------------------
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
why? :)


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
Jul 17 '05 #8
Martin Lucas-Smith wrote:

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


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.

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


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 :)





Jul 17 '05 #9
.oO(icui)
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.
At least that's the definition of an empty URL. Some older browsers
interpret it differently (current directory instead of current page).
but it's not reasoneable that a page php with 3 null images (for a
stupid error :) ) runs x 3.
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.
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 :)


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

Micha
Jul 17 '05 #10
icui <ic**@noemail.it> wrote:

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.
That's your opinion. It happens to be wrong. Do not expect standards to
change because you misunderstood them.
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 :)


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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 17 '05 #11
icui wrote:
In this case: <form action="">

the semplification "" = current page


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:

http://www.ietf.org/rfc/rfc2396.txt

Cheers!

--
Jock
Jul 17 '05 #12
Michael Fesser wrote:
.oO(i-c-u-i)
----------------------------------------
page.htm

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

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

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

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
[ ... ]
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).


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

--
Jock
Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: beliavsky | last post by:
I can define a function that transforms either a scalar or each element in a list as follows: def twice(x): try: return map(twice,x) except: return 2*x print twice(3) # 6
18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
15
by: Liviu | last post by:
Say i have a function: char *rmtrail(char *str) { if (str) { int i; for (i = strlen(str) - 1; i >= 0 && isspace(str); --i) ;
10
Sheepman
by: Sheepman | last post by:
I write most of my programs using only void function. They end up long and bloated because I often write the same code over and over again in each void function. I'm even confused by void functions...
10
by: Jesse Burns aka jburns131 | last post by:
Here's my code: $today = getdate(); $to = someone@somewhere.com; $subject = "New Application: ".$userdata." - ".$today."/".$today."/".$today; $message_header = "New Application:...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
2
by: presencia | last post by:
Hi everyone, I have written a small program running fine until a Segmentation fault. Using a debugger I have localized the function the fault happens in. The Signal appears during a call to delete...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.