What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Tiny winy PHP question :P

Tiny winy PHP question :P
Author: Message:
ayjay
Senior Member
****

Avatar

Posts: 850
Reputation: 58
– / Male / Flag
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
Profile PM Find Quote Report
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
Profile E-Mail PM Find Quote Report
absorbation
Elite Member
*****

Avatar

Posts: 3636
Reputation: 81
– / Male / Flag
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: :P

mysql_query("UPDATE views SET views=(views + 1) WHERE id='1");
03-19-2006 03:53 PM
Profile PM Find Quote Report
RaceProUK
Elite Member
*****

Avatar

Posts: 6073
Reputation: 57
39 / Male / Flag
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.
[Image: spartaafk.png]
03-19-2006 04:02 PM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
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
Profile E-Mail PM Find Quote Report
ayjay
Senior Member
****

Avatar

Posts: 850
Reputation: 58
– / Male / Flag
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 [Image: msn_tongue.gif]

I'll try some of these options, thanks [Image: msn_happy.gif]
03-19-2006 04:41 PM
Profile PM Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
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
Profile PM Web Find Quote Report
ayjay
Senior Member
****

Avatar

Posts: 850
Reputation: 58
– / Male / Flag
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 [Image: msn_tongue.gif]
Thanks.
03-20-2006 08:23 PM
Profile PM Find Quote Report
hmaster
Senior Member
****

Avatar

Posts: 716
Reputation: 24
33 / Male / Flag
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 :)
[Image: sig.png]
03-20-2006 09:00 PM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On