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 ()
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 is to use fopen () to open the file. After N attempts, the following procedures are proved to be feasible and verified in the billable space.
Count Directory
| --- Count. php
| ---Counter.txt
| --- 0 ~ 9th digital gif image (0.gif, ...9.gif)
Reference content is as follows: 1 2/* This file is count \ conut. php */ 3 4 $ countfile = "count/counter.txt "; 5 6if (! File_exists ("$ countfile ")){ 7 $ fp = fopen ("$ countfile", "w "); 8 $ num = 1; 9 fputs ($ fp, $ num, 8 ); 10 fclose ($ fp ); 11} 12 else { 13 $ fp = fopen ("$ countfile", "rw + "); 14 $ num = fread ($ fp, 8 ); 15 $ num ++; 16 rewind ($ fp ); 17 fputs ($ fp, $ num, 8 ); 18 fclose ($ fp ); 19} 20 21/* if you do not need to display numbers in the image, convert the following Until?> Change the previous part to echo "". $ num. ""; and so on */ 22 $ len_str = strlen ($ num ); 23for ($ I = 0; $ I <$ len_str; $ I ++ ){ 24 $ numbers_exploded = substr ($ num, $ I, 1 ); 25 $ output_str = $ output_str .""; 26} 27 echo $ output_str; 28?> |
Usage: Web teaching network https://www.php1.cn for example, create a test. php outside the count folder. For other locations, modify the $ countfile path.
Directly require the count. php file where test. php is to be displayed (note the path ).
Reference content is as follows:
Echo "you are the first "; Require ("count/count. php "); Echo "guest "; ?> |
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.