473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

strange repeating of fwrite (because of javascript?)

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
4 3829

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
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Leif Wessman | last post by:
I'm using proc_open to write to a perl program. Sometimes (not always) the fwrite function below takes about 10 seconds to complete. Most of the time it takes about 0.001 seconds. I don't know what...
9
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using the iSeries Client Access Driver ver...
17
by: SW1 | last post by:
I wrote a small program which does something like tftp - transfering files and some chat, anyway i got a problem with fwrite, here is a snippet of my code: while(length > 0) { putchar('.');...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
4
by: welch | last post by:
while taking some rough disk performance measures on windows machines, and snooping with FileMon, i've noticed some odd behavior here's the little nul-writer i'm running: def writeTest(nBlocks,...
3
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
4
by: pmz | last post by:
Dear Group, I'm not sure what is going one with my software, but something really strange. I'm storing in BLOB in MySQL data of an image, which I display like: header("Content-Type:...
112
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not...
25
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.