How to add a counter in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags php3 file
Describes how to add a counter in PHP. When we set up a website, some webmasters will need to add counters on their home pages. today we will introduce the following code to counter. inc :? * | When we build a website, some webmasters may need to add counters on their home pages. today we will talk about it in detail.Counter. inc contains the following code:

  1. /*
  2. | A simple counter
  3. */
  4. Function get_hitcount ($ counter_file)
  5. {
  6. /* Returns the counter to zero.
  7. In this case, if the counter is not used, the initial value is 1.
  8. Of course, you can set the initial value to 20000 to cheat me.
  9. */
  10. $Count=0;
  11. // If the stored counter file already exists, read the content
  12. If (file_exists ($ counter_file ))
  13. {
  14. $Fp=Fopen($ Counter_file, "r ");
  15. // We only took the first 20 digits. I hope your site will not be too popular.
  16. $Count=0+ Fgets ($ fp, 20 );
  17. // Because the function fgets () returns a string, we can automatically convert it to an integer by adding 0.
  18. Fclose ($ fp );
  19. // File operation completed
  20. }
  21. // Increase the count value once
  22. $ Count ++;
  23. // Write the new count value to the file
  24. $Fp=Fopen($ Counter_file, "w ");
  25. Fputs ($ fp, $ count );
  26. Fclose ($ fp );
  27. # Return count value
  28. Return ($ count );
  29. }
  30. ?>

After adding a counter in PHP, we need to change the front. php3 file to display this counter:

  1. Include("Include/counter. inc ");
  2. // I put the counter value in the counter.txt file, read and output
  3. Printf ("<CENTER><B>% 06d B> CENTER> <BR>N ",
  4. Get_hitcount ("counter.txt "));
  5. Include ("include/footer. inc ");
  6. ?>

All the above code is the PHP counter adding implementation method, hope to have some help to friends in need.

Counter. inc contains the following code :? /* | A simple...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.