473,543 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Jul 17 '05 #1
12 8859
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_f ile)) {
echo 'Failed to copy.<br />';
}
//$URL_CSS_='stri ng';
?>
<link rel="stylesheet " href="<?=$URL_C SS_?>" 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_f ile)) {
echo 'Failed to copy.<br />';
}
//$URL_CSS_='stri ng';
?>
<link rel="stylesheet " href="<?=$URL_C SS_?>" 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_C SS_?>" 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_C SS_?>" 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

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

Similar topics

9
3234
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
8902
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 to create multiple functions which they look almost identical in C++ source code. The C++ compiler should be able to compile one Test() function...
16
16207
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
8810
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* pParam)
15
2246
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
1592
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 though. The book says void functions don't return data but I make them do it. I'm using a void function to send data to a file and the screen. I use a...
10
16499
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: ".$userdata;
6
5884
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 or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I...
2
3339
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 in the recursive function I have posted below. I have done some C coding before but I am fairly new to C++. I would really appreciate any help. I can't...
0
7399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7729
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7677
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5258
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3385
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
952
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
626
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.