The general counter program executes the external program function exec (), but many host spaces (including charged) refuse this dangerous operation.
We have to use fread (), fwrite (), and other file read/write functions. The key lies in which method fopen () is used to open the file. After N attempts, it is confirmed that the following procedures are feasible and have been verified in the billable space.
Count directory
| --- Count. php
| ---Counter.txt
| --- 0 ~ 9th digital GIF image (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 need to display numbers in the image, convert the following until?> In the previous section, change to echo "<font color = red>". $ num. "</font>"; and so on */
$ Len_str = strlen ($ num );
For ($ I = 0; $ I <$ len_str; $ I ){
$ Numbers_exploded = substr ($ num, $ I, 1 );
$ Output_str = $ output_str. " ";
}
Echo $ output_str;
?>
Usage:
For example, create a new test. php file outside the count folder. For other locations, modify the $ countfile path.
Directly require the count. php file (focusing on the path) Where test. php is to be displayed ).
<Br>
<? Php
Echo "you are the first ";
Require ("count/count. php ");
Echo "guest ";
?>
<Br>
Tune the last question. Set the counter.txt file to read and write. In Windows, the File Permission is everyone, and in Linux it is at least 444. For files on the Internet or files in Linux, you must use the FTP software.