473,911 Members | 6,170 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 8894
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
3252
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
8937
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 into two almost identical functions before they are translated into the machine language object. ...
16
16317
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
8838
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
2276
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
1624
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 void function to create the random number array. Then the other void functions use the random array,...
10
16615
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
5945
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 even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
2
3362
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 figure out where the problem is. The function the SIGSEGV appears in is the following. It...
0
9879
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11349
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11055
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9727
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8099
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7250
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5939
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6142
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3360
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.