Shoutbox

Tiny winy PHP question :P - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Tiny winy PHP question :P (/showthread.php?tid=57185)

Tiny winy PHP question :P by ayjay on 03-19-2006 at 03:34 PM

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;

?>

RE: Tiny winy PHP question :P by Josh Loughlin on 03-19-2006 at 03:52 PM

i dont understand bu the font goes bigger after refreshing once


RE: Tiny winy PHP question :P by absorbation on 03-19-2006 at 03:53 PM

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: :P

mysql_query("UPDATE views SET views=(views + 1) WHERE id='1");


RE: Tiny winy PHP question :P by RaceProUK on 03-19-2006 at 04:02 PM

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.
RE: Tiny winy PHP question :P by J-Thread on 03-19-2006 at 04:07 PM

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?
RE: Tiny winy PHP question :P by ayjay on 03-19-2006 at 04:41 PM

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 [Image: msn_tongue.gif]

I'll try some of these options, thanks [Image: msn_happy.gif]


RE: Tiny winy PHP question :P by WDZ on 03-20-2006 at 05:18 AM

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


RE: Tiny winy PHP question :P by ayjay on 03-20-2006 at 08:23 PM

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 [Image: msn_tongue.gif]
Thanks.
RE: Tiny winy PHP question :P by hmaster on 03-20-2006 at 09:00 PM

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 :)