By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 879 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

strange repeating of fwrite (because of javascript?)

P: n/a
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

Apr 24 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Nate Murray wrote:
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.

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

<script language="JavaScript">

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

</script>

</head>


Hey there

You were spot on about it being javascript, I think the problem is
img.src='';

Done a quick test and it seems both firefox and explorer refuse to let
img.src be a blank string and automatically set img.src to something
nonblank if you try. In firefox img.src was set to
'http://server/pagedir/testpage.html' immediately after img.src='', in
explorer img.src was 'http://server/pagedir/'. (it was x.gif before)

Do alert(img.src) just after img.src='' on your page to see what I
mean.

I think this is why the php script will be called twice, once when you
open the page in your browser, the second time when the setting
img.src='' ends up trying to get the current page (why? silly
browser..)

The solution is set img.src to anything else apart from a blank string,
even setting img.src to a fake filename of an img that doesnt exist
should be ok. I hope..

Seeya

Tim

Apr 24 '06 #2

P: n/a
"tihu" <ti********@gmail.com> wrote:

You were spot on about it being javascript, I think the problem is
img.src='';

Done a quick test and it seems both firefox and explorer refuse to let
img.src be a blank string and automatically set img.src to something
nonblank if you try. In firefox img.src was set to
'http://server/pagedir/testpage.html' immediately after img.src='', in
explorer img.src was 'http://server/pagedir/'. (it was x.gif before)
Yes, of course it is. This should not be a surprise, nor should it be
browser-dependent. This is a relative URL, which means that the blank
string is relative to the URL of the page it came from.
I think this is why the php script will be called twice, once when you
open the page in your browser, the second time when the setting
img.src='' ends up trying to get the current page (why? silly
browser..)


Not "silly browser". Silly programmer. That is exactly what is supposed
to happen.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Apr 25 '06 #3

P: n/a

Tim Roberts wrote:
"tihu" <ti********@gmail.com> wrote:

You were spot on about it being javascript, I think the problem is
img.src='';

Done a quick test and it seems both firefox and explorer refuse to let
img.src be a blank string and automatically set img.src to something
nonblank if you try. In firefox img.src was set to
'http://server/pagedir/testpage.html' immediately after img.src='', in
explorer img.src was 'http://server/pagedir/'. (it was x.gif before)
Yes, of course it is. This should not be a surprise, nor should it be
browser-dependent. This is a relative URL, which means that the blank
string is relative to the URL of the page it came from.


Why did they come up with different urls?
I think this is why the php script will be called twice, once when you
open the page in your browser, the second time when the setting
img.src='' ends up trying to get the current page (why? silly
browser..)


Not "silly browser". Silly programmer. That is exactly what is supposed
to happen.


Patronising git

Apr 25 '06 #4

P: n/a
Yes, of course it is. This should not be a surprise nor should it be
browser-dependent. This is a relative URL, which means that the blank
string is relative to the URL of the page it came from.


It is browser dependent.

Checked on firefox, explorer and opera, they all come up with different
values for img.src. Explorer does give a relative url ( the current
directory + '' ), firefox uses the current url ( not quite a relative
url, but close ), in opera it is a blank string.

Explain that

Tim

Apr 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.