Call WordPress function Statistic article traffic and the implementation of PHP native counter _php instance

Source: Internet
Author: User

1. First introduces WordPress's two powerful plug-ins:

(1) Count per day is a very powerful number of visitors statistics plug-ins, can be counted daily, yesterday, weekly, monthly and so on the number of visitors (based on IP statistics), statistics online visitors, browsers, search terms, and so on, with a variety of call simple code, template tags and gadgets, Facilitate your own invocation and integration.
(2) STATPRESSCN, real-time display blog access statistics, perfect support in Chinese (such as search keywords, etc.). It can focus on visitors, reptiles, search keywords, subscription statistics, browsers, operating systems and other information to facilitate your timely grasp of the blog access, but also provide customized output, statistical rights, information storage period, such as personalization options. In addition, you can always grasp the status of the blog is subscribed to and put it in the sidebar display, you can also get all the pages (including post, page, archive and homepage) access to the situation, you can also display in the sidebar in the blog is the most visited the most popular articles. Increased localized language support for mainland China and Taiwan (currently supported in wp-config.php to ZH_CN, ZH_HK, and ZH_TW).
The way to install Plug-ins is very powerful, but the drawbacks are obvious-easy to affect the speed of the Web page loading, so I gave up this way.

2. Second, is a simple few code, through the statistics page refresh number to record the number of visitors, the following code into the 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, ' views ', true);
 if (!update_post_meta ($post _id, ' views ', ($post _views+1)))
 {
 Add_post_meta ($post _id, ' views ', 1, true);
 }
 }
 }
}
Add_action (' Wp_head ', ' record_visitors ');
 
Function name: Post_views
///function: Get the reading number of the article Functions
post_views ($before = ' (click ', $after = ' times) ', $echo = 1)
{
   global $post;
 $post _id = $post->id;
 $views = (int) Get_post_meta ($post _id, ' views ', true);
 if ($echo) echo $before, Number_format ($views), $after;
 else return $views;
}

Then call on the screen that needs to be displayed, such as footer.php or header.php:

<?php post_views (', ' Times ');?>

3.PHP Native Implementation Access counter example:
index.php

<?php
 include ("counter.php")
?>

 
 

counter.php

<?php
 //$five, $four the number of variables representing zeros, placed in front of the number to form 6-bit
 function Counter ()
 {         //definition function
  $five = "00000";          DECLARE variable
  $four = "0000";
  $three = ";
  " $two = "a";
  $one = "0";
  $counter = "Record.dat";      The destination file that holds the number of accesses,. dat format
  if (!file_exists ($counter))     //To determine whether the file exists
  {
   $counter = fopen ($counter, "w");
   Fputs ($counter, "0");      Write to File
   fclose ($counter)
  ;
  else
  {
   $fp = fopen ($counter, "r+");
   $num = fgets ($fp, "1024");    If the file exists, read out the file and add 1
   $num = $num + 1;
   if ($num <)
    print "$five". $num ";
   ElseIf ($num <)
    print "$four". $num ";
   ElseIf ($num < 1000)
    print "$three". $num ";
   ElseIf ($num < 10000)
    print "$two". $num ";
   ElseIf ($num < 100000)
    print "$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.