Call the WordPress function to count the document traffic and implement the PHP native counter

Source: Internet
Author: User
This article mainly introduces how to call the WordPress function to count the access volume and implement the PHP native counter. The author also mentions the two plug-ins in WordPress. However, we do not recommend plug-ins for performance considerations, for more information, see 1. first, we will introduce two powerful plug-ins of WordPress:

(1) Count per Day is a very powerful plug-in for counting the number of visitors every Day, yesterday, every week, and every month (based on IP addresses ), counts the number of online visitors, browsers, search terms, and so on. it comes with a variety of simple call codes, template tags, and gadgets to facilitate your own calls and integration.
(2) StatPressCN: displays the access statistics of the blog in real time, and supports Chinese characters (such as search keywords ). It can display visitors, crawlers, search keywords, subscription statistics, browsers, operation systems, and other information in a centralized manner, so that you can grasp the blog access information in a timely manner, it also provides custom settings such as output, statistics permission, and information storage period. In addition, you can view the status of a blog subscribed to at any time and display it in the sidebar. you can also obtain access to all pages (including post, page, archive, and homepage, you can also display the most popular articles in the blog on the sidebar. Added localization language support for mainland China and Hong Kong and Taiwan (currently supported in wp-config.php is set to zh_CN, zh_HK and zh_TW ).
The plug-in installation method is powerful, but its drawbacks are also obvious-it is easy to affect the webpage loading speed, so I gave up this method.

2. a few simple code records the number of visitors by counting the number of page refreshes. put the following code in function. php:

/* Access count */function record_visitors () {if (is_singular () {global $ post; $ post_ID = $ post-> ID; if ($ post_ID) {$ post_views = (int) get_post_meta ($ post_ID, 'view', true); if (! Update_post_meta ($ post_ID, 'view', ($ post_views + 1) {add_post_meta ($ post_ID, 'view', 1, true );}}}} add_action ('WP _ head', 'record _ visitors '); // Function name: post_views // function: function post_views ($ before = '(click', $ after = 'Times)', $ echo = 1) {global $ post; $ post_ID = $ post-> ID; $ views = (int) get_post_meta ($ post_ID, 'view', true); if ($ echo) echo $ before, number_format ($ views), $ after; else return $ views ;}

Call the following interface on the page to be displayed, such as footer. php or header. php:

<? Php post_views ('', 'sub');?>

3. example of PHP native access counters:
Index. php

<? Php include ("counter. php")?>  Access counters    
 
  
Visitor counter
                   
   You are the first<? Php Counter ()?>  Visitors  
  

Counter. php

<? Php // $ five, $ four, and other variables represent the number of zeros. put them in front of the number to form a six-digit function Counter () {// defines the function $ five = "00000 "; // declare the variable $ four = "0000"; $ three = "000"; $ two = "00"; $ one = "0"; $ counter = "record. dat "; // the object that stores the number of shards ,. if (! File_exists ($ counter) // judge whether the file exists {$ counter = fopen ($ counter, "w"); fputs ($ counter, "0 "); // write the file fclose ($ counter);} else {$ fp = fopen ($ counter, "r +"); $ num = fgets ($ fp, "1024 "); // if the file exists, read the file and add 1 $ num = $ num + 1; if ($ num <10) print "$ five ". "$ num"; elseif ($ num <100) print "$ four ". "$ num"; elseif ($ num <1000) print "$ three ". "$ num"; elseif ($ num <10000) print "$ two ". "$ num"; elseif ($ num <100000) prin T "$ one ". "$ num"; else print "$ num" ;}$ fp = fopen ($ counter, "w"); fputs ($ fp, "$ num "); fclose ($ fp) ;}?>

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: info-contact@alibabacloud.com 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.