Home >Backend Development >PHP Tutorial >PHP counter program_PHP tutorial
1) Text counter
$countfile="/count.txt"; //Set the file to save data
if (!file_exists($countfile)){//Determine whether the file exists
exec( "echo 0 > $countfile");
}
$fp = fopen($countfile,"rw");
$length=filesize($countfile);
$num = fgets($fp,$length);
$num += 1;
exec( "rm -rf $countfile");
exec( "echo $num > $countfile") ;
print "Total visits:"."$num"."person times"; //Display the number of visits
?>
2) Graphical counter
$countfile ="/count-num.txt"; //Set the file to save data
if (!file_exists($countfile)) //Determine whether the file exists
{exec( "echo 0 > $countfile") ;}
$fp = fopen($countfile,"rw");
$length=filesize($countfile);
$num = fgets($fp,$length);
$num += 1;
exec( "rm -rf $countfile");
exec( "echo $num > $countfile");
$len_str = strlen($num);
for ($i=0;$i<$len_str;$i++){
$each_num = substr($num,$i,1);
$out_str = $out_str . "";
}
print "Total visits:"."$out_str"."Persons"; //Display the number of visits
?>