tags:

views:

55

answers:

2

Hi

I made a website that takes a value from the user and saves it to a text file. On my home PCs, it works great, and the data.txt file gets updated instantly.

However, my friend says it is not getting instantly updated on his home PCs [he is using a Wi-Max connection]. It takes 1 minute to get updated for him.

Is this problem a result of this code that I am using?

<?php


  $command = $_POST['command'];

     $ourFileName = "data.txt";
unlink($ourFileName);

 $fileHandle = fopen($ourFileName, 'w') or die("can't open file");

 $stringData = $command;

 fwrite($fileHandle, $stringData);

 fclose($fileHandle);

 header ("Location: index.html"); 
?>

How is it possible to fix this problem?

I'm sorry if this question is not supposed to be posted on stackOverflow, I just did not know where else to post it.

+2  A: 

Try using file_put_contents()

<?php
$command = $_POST['command'];
file_put_contents('data.txt', $command);
header ("Location: index.html"); 
?>

Additional, it could be an idea to validate the command first.

Also, try adding no cache headers (your friends browsers may cache index.html)

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
PHP_Jedi
the header code did not fix the problem, it is probably because of his connection.
Hazem
A: 

There's a gian lag involved when using cell connections. How large is the string involved being written to a file? If it's minimal I'd say it's the network lag, not your code.

Where's this site running? Your home PC or a "real" webserver?

jlindenbaum
real webserver, small amount of txt
Hazem
Then it's the latency. My 3G network usually has a 700ms+ latency and gets about 1000kbps down, 150kbps up. It's a lot slower than a land connection
jlindenbaum