A nasty bug in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pyc
Forum Newbie
Posts: 2
Joined: Thu Jun 01, 2006 4:37 pm

A nasty bug in PHP?

Post by pyc »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


try this code, you'll have to have file named picture.gif in the same directory where is the script executed...

Code: Select all

<?
$n = $_SERVER[DOCUMENT_ROOT].'/test.txt';
if (file_exists($n)) {
    $f = fopen($n, 'r+');
}    
else {
    $f = fopen($n, 'x+');
    fwrite($f, '0');
    rewind($f);
}    
$d = fread($f,5);
$d++;
rewind($f);
fwrite($f, $d);
fclose($f); 
//header('Content-type: image/gif');
//readfile($_SERVER[DOCUMENT_ROOT].'/picture.gif');
?>
You'll see no output except one variable in file test.txt is incremented by one and written back to file. I made this for counting purposes...

Now de-comment last two lines. Now picture.gif shows in browser which is intended, but the incrementation of variable is now by 2, always when you load script!!! I really can't explain this.... Can you? I please you people to try yourself...

Strange enough, it seems that there's no such error on Windows PHP ... I tried it on Linux, PHP 4.4.2


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I really can't explain this.... Can you?
write the contents of $_SERVER array to the log file, then analise the log. It could be caused by browser sending two requests for some reason (related to IE image flicker problem or something)
pyc
Forum Newbie
Posts: 2
Joined: Thu Jun 01, 2006 4:37 pm

Post by pyc »

adding these headers into the code

header('Content-length.....
header('Expires:....

..solved the problem, but I still don't see how :)
Post Reply