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.



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;



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]





Leave a Reply

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