When we build a website, some webmasters will need to add counters on their home pages. Let's talk about it today.Counter. inc contains the following code:
- <?
- /*
- | A simple counter
- */
- Function get_hitcount ($ counter_file)
- {
- /* Returns the counter to zero.
- In this case, if the counter is not used, the initial value is 1.
- Of course, you can set the initial value to 20000 to cheat me.
- */
- $Count=0;
- // If the stored counter file already exists, read the content
- If (file_exists ($ counter_file ))
- {
- $Fp=Fopen($ Counter_file, "r ");
- // We only took the first 20 digits. I hope your site will not be too popular.
- $Count=0+ Fgets ($ fp, 20 );
- // Because the function fgets () returns a string, We can automatically convert it to an integer by adding 0.
- Fclose ($ fp );
- // File operation completed
- }
- // Increase the Count value once
- $ Count ++;
- // Write the new count value to the file
- $Fp=Fopen($ Counter_file, "w ");
- Fputs ($ fp, $ count );
- Fclose ($ fp );
- # Return Count value
- Return ($ count );
- }
- ?>
After adding a counter in PHP, we need to change the front. php3 file to display this counter:
- <?
- Include("Include/counter. inc ");
- // I put the counter value in the counter.txt file, read and Output
- Printf ("<CENTER><B>% 06d</B></CENTER> <BR>N ",
- Get_hitcount ("counter.txt "));
- Include ("include/footer. inc ");
- ?>
All the above Code is the PHP counter adding implementation method, hope to have some help to friends in need.