Useful PHP code – guestbook counter

[problem]

You want to display a counter on your web page, but you do not want to use a db.

[/problem]

[solution]

All you need to make this work is echo “1” into a file called counter and ensure the web user has write access. For example: $ echo 1 > counter; chmod 777 counter;

[/solution]

[example]

<?php

$fh=fopen(“/tmp/counter.txt”, “r”);
$cnt=fread($fh,filesize(“/tmp/counter.txt”));
fclose($fh);
$fh=fopen(“/tmp/counter.txt”, “w+”);
if(fputs($fh,($cnt+1))) { print(“You are visitor: $cntn”); }
fclose($fh);

?>


[/example]

[reference]

[tags]PHP Guest Book, PHP Coding School[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *