You are here: strange repeating of fwrite (because of javascript?) « PHP Programming Language « IT news, forums, messages
strange repeating of fwrite (because of javascript?)

Posted by Nate Murray on 04/24/06 20:57

Hey all, I'm having a strange PHP (4.3.10) problem that seems to have
something
to do with javascript. This is a bit complex, so hold on to your hats.

My issue is that a single function is wrongly being called twice. The
weird
part is that no "print" functions are being repeated, but any "lower"
functions,
such as fwrite or Pear::Mail::send are repeated twice. It is the
strangest
thing.

It has something to do with the javascript creating an image. It is as
if
the php code runs, the page loads, and the php code runs again. Sort
of.
You would think that if the code was running twice it would print
"ASDFG"
twice to the browser, but it doesn't. (Also, if this code is run
sending
mail with Pear::Mail it sends the mail twice, but does not output the
timestamp
twice.) - If you remove the javascript the php runs fine -. This is
strange to me because php is server side and javascript is client side.
I wouldn't have thought anything in javascript could interact with PHP.


This code *should*:
1) print "ASDFG"
2) sleep
3) print "Control [timestamp]"
4) write [timestamp] to the file and be done

However, this is what the code does:
1) print "ASDFG"
2) sleep
3) print "Control [timestamp]"
4) write [timestamp] to the file
5) sleep 2 sec
6) write [timestamp+2] to the file

To test it try this:
1) save the php code
2) tail -f tmp.txt
3) load the page and watch. You will see only 1 output to the browser
but 2
entries appear in tmp.txt

Here is the code:
------------------

<html> <head> <title>Writing Twice</title>

<script language="JavaScript">

function preloadImages() {
img = new Image;
img.src = "";
// img.src = "img.gif";
}

</script>

</head>
<body onLoad="preloadImages('')">

<?php
function testForm()
{
$somecontent = date("r") . "\n";
print "Control: $somecontent <br />";
$filename ="/var/www/htdocs/tmp/tmp.txt";

$handle = fopen($filename, 'a');
fwrite($handle, $somecontent);
fclose($handle);
}

print "ASDFG<br>\n";
sleep(2);
testForm();
?>

</body> </html>


--------- End Code --------

This is an odd problem, I'd be grateful for any solutions or
suggestions you
guys might have.

-Nate Murray
http://www.natemurray.com


p.s.
Possible solutions that are not options:

1) upgrading PHP - if this is a bug, I can't upgrade because it's a
hosted
server that I can't upgrade.

2) removing the javascript completely - This code is part of a bigger
template and totally removing this javascript code is not an option,
though
modifying it is an option

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация