Vote count:
0
Just trying to count the number of times a button has been clicked, record that to a file so it works for everyone. It's reading the file, but ain't writing. Ideas?
<form action='' method="post">
<input type="submit" name="submit" value="Tweet it!" onclick="genTweet()"/>
</form>
<br>
<?php
$f = fopen('counter.txt', 'r+'); // use 'r+' instead
flock($f, LOCK_EX); // avoid race conditions with concurrent requests
$total = (int) fread($f, max(1, filesize('counter.txt'))); // arg can't be 0
/*if someone has clicked submit*/
if (isset($_POST['submit'])) {
rewind($f); // move pointer to start of file so we overwrite instead of append
fwrite($f, ++$total);
}
fclose($f);
?>
This button has been clicked <?php echo $total; ?> times.
asked 56 secs ago
Counting button clicks with PHP
Aucun commentaire:
Enregistrer un commentaire