Tiny winy PHP question :P |
Author: |
Message: |
ayjay
Senior Member
Posts: 850 Reputation: 58
– / /
Joined: Mar 2004
|
O.P. Tiny winy PHP question :P
Just a little test script to increase the number by 1 every time the page is refreshed and it works fine, but if you hold down F5 to refresh for quite a while, the number decreases and I can't work out why :/
code: <?php
$filename = "count.txt";
$x = file_get_contents("$filename");
file_put_contents("$filename", $x);
$x = $x*1;
$y = $x+1;
$f = fopen($filename, "w");
fwrite($f, $y);
fclose($f);
echo $x;
?>
This post was edited on 03-19-2006 at 03:34 PM by ayjay.
|
|
03-19-2006 03:34 PM |
|
|
Josh Loughlin
Junior Member
Posts: 19
Joined: Mar 2006
|
RE: Tiny winy PHP question :P
i dont understand bu the font goes bigger after refreshing once
|
|
03-19-2006 03:52 PM |
|
|
absorbation
Elite Member
Posts: 3636 Reputation: 81
– / /
Joined: Feb 2005
|
RE: Tiny winy PHP question :P
Wait I am confused with all these mix of varibles, $x and $y, but it should work fine. Maybe it is your browser problem. I try not to use txt files but mysql:
mysql_query("UPDATE views SET views=(views + 1) WHERE id='1");
|
|
03-19-2006 03:53 PM |
|
|
RaceProUK
Elite Member
Posts: 6073 Reputation: 57
39 / /
Joined: Oct 2003
|
RE: Tiny winy PHP question :P
Wouldn't
code: <?php
$filename = "count.txt";
$x = file_get_contents("$filename");
$x = $x+1;
file_put_contents("$filename", $x);
echo $x;
?>
be simpler?
Or, if there's a PHP equivalent of C's scanf, use that to read the number from the file, and a printf equivalent to write it.
|
|
03-19-2006 04:02 PM |
|
|
J-Thread
Full Member
Posts: 467 Reputation: 8
– / / –
Joined: Jul 2004
|
RE: Tiny winy PHP question :P
code: <?php
$filename = "count.txt";
$x = intval(file_get_contents($filename)) + 1;
$f = fopen($filename, "w");
fwrite($f,$x);
fclose($f);
echo $x;
?>
How about that?
This post was edited on 03-19-2006 at 04:09 PM by J-Thread.
|
|
03-19-2006 04:07 PM |
|
|
ayjay
Senior Member
Posts: 850 Reputation: 58
– / /
Joined: Mar 2004
|
O.P. RE: Tiny winy PHP question :P
There are probably loads of easier ways than I used but I wrote this after 4 hours of learning so I still don't know much
I'll try some of these options, thanks
|
|
03-19-2006 04:41 PM |
|
|
WDZ
Former Admin
Posts: 7105 Reputation: 107
– / /
Joined: Mar 2002
|
RE: Tiny winy PHP question :P
For a counter script, it's also a good idea to use file locking to prevent multiple instances of the script from messing with the file at the same time.
See http://php.net/flock
|
|
03-20-2006 05:18 AM |
|
|
ayjay
Senior Member
Posts: 850 Reputation: 58
– / /
Joined: Mar 2004
|
O.P. RE: Tiny winy PHP question :P
quote: Originally posted by WDZ
For a counter script, it's also a good idea to use file locking to prevent multiple instances of the script from messing with the file at the same time.
See http://php.net/flock
Ah great. That'll probably be why it goes dodgy if you hold down F5
Thanks.
|
|
03-20-2006 08:23 PM |
|
|
hmaster
Senior Member
Posts: 716 Reputation: 24
33 / /
Joined: Nov 2004
|
RE: Tiny winy PHP question :P
Heres a commented version of a 'hit logger'
code: <?php
// Define the text file to store data
$HitFile = "logger.txt";
// Open the file for reading and writing
$handle = fopen($HitFile, "r+");
// Lock the file for exclusive access (protects from data coruption)
flock($handle, LOCK_EX);
// Get the contents of the file (the old hit count)
$oldcount = fgets($handle);
// Increment 1 to the old count
$newcount = $oldcount + 1;
// Set the file head to the begining (destroys the old data)
rewind($handle);
// Write the new count to the file
fwrite($handle, $newcount);
// Unlock the file
flock($handle, LOCK_UN);
// Close the file
fclose($handle);
// Display the new hit count
echo $newcount;
?>
logger.txt should have 0 to begin with
|
|
03-20-2006 09:00 PM |
|
|
|