PHP counter implementation code

  • 2020-06-12 08:38:49
  • OfStack


<?php
/* A simple implementation of using text files to record data */
$counter=1;
if(file_exists("mycounter.txt")){
$fp=fopen("mycounter.txt","r");
$counter=fgets($fp,9);
$counter++;
fclose($fp);
}
$fp=fopen("mycounter.txt","w");
fputs($fp,$counter);
fclose($fp);
echo "<h1> You are the first ".$counter." Time to visit this page! <h1>";
?>


<?php
// The following one USES a simple counter based on the database. No additional precautions have been added 1 The method that the person repeats refresh. For reference only. 
$conn=mysql_connect("localhost","root","abc");
$result=mysql_query("use db_counter");
$re=mysql_query("select * from tb_counter");
$result=mysql_fetch_row($re);
$counter=$result[0];
echo " You are the first {$counter} A visitor! ";
$counter+=1;echo "<hr>{$counter}";
mysql_query("update tb_counter set counter=$counter");
mysql_close($conn);
?>


Related articles: