General counter programs use the external program function exec(), but many host spaces (including paid ones) reject this dangerous operation.
We have to use fread(), fwrite() and other file reading and writing functions. The key lies in which method fopen() is used to open the file. After trying N times, it was confirmed that the following procedure is feasible and has been verified in the paid space.
count directory
|---count.php
|---counter.txt
|---Number GIF pictures from 0~9 (0.gif,...,9.gif)
/* This file is countconut.php */
$countfile = "count/counter.txt";
if (!file_exists("$countfile")){
$fp=fopen("$countfile","w");
$num=1;
fputs($fp,$num,8);
fclose($fp);
}
else{
$fp=fopen("$countfile","rw ");
$num=fread($fp,8);
$num ;
rewind($fp);
fputs($fp,$num,8);
fclose($fp);
}
/*If you do not use pictures to display numbers, please change the following part until ?> to something like echo "".$num."";*/
$len_str = strlen($num);
for($i=0;$i<$len_str;$i ){
$numbers_exploded = substr($num,$i,1);
$output_str = $output_str . "";
}
echo $output_str;
?>
How to use:
For example, create a new test.php outside the count folder. Please modify the $countfile path in other locations.
Directly require the count.php file where test.php is to be displayed (pay attention to the path).
echo "You are No.";
require("count/count.php");
echo "Visitors";
?>
To emphasize the last question, please set the counter.txt file to be readable and writable. The file permissions are everyone under Windows and at least 444 under Linux. To set file permissions on the Internet or under Linux, you need to use FTP software.