Ajax provides an example of the document access statistics function for static pages.

Source: Internet
Author: User

Ajax provides an example of the document access statistics function for static pages.

This example describes how to use Ajax to collect statistics on static pages. We will share this with you for your reference. The details are as follows:

As we all know, static pages are not only fast, but also helpful for seo. A few days ago, I wrote a post about static WordPress pages to KVDB on the SAE platform. After I used it for a while, I found that the speed was indeed obvious. However, one problem that arises is that after the static content of the article, the page will not be processed by the WordPress program, which leads to invalid access statistics of the article. Of course, a plug-in called wp-postview can solve this problem, but I do not like the plug-in very much because it will slow down the overall speed. So here is a solution, that is, to use Ajax for statistics, also based on the SAE platform.

Define document access statistics

In fact, I have mentioned in my previous post that KVDB + TaskQueue implements efficient counters. Just make a simple modification to this. Because it is not processed by php, the queue service cannot be used for counting. The counting class is also defined and put under the root directory of the website:

$ Countkey = $ _ GET ['key']; // obtain the Count keyif ($ countkey = "") to be operated exit; if ($ _ GET ['action'] = "add") {$ cou = new counter ($ countkey); $ cou-> inc (); // Add 1} elseif ($ _ GET ['action'] = "get") {$ cou = new counter ($ countkey ); echo $ cou-> get ();} class counter {private $ kvdb; private $ key; public function _ construct ($ key) {$ this-> kvdb = new CKvdb (); $ this-> key = $ key;} public function inc () {$ num = $ this-> kvdb-> get ($ this-> key) + 1; $ this-> kvdb-> set ($ this-> key, $ num); return $ num;} public function dec () {$ num = $ this-> kvdb-> get ($ this-> key)-1; $ this-> kvdb-> set ($ this-> key, $ num); return $ num;} public function get () {$ num = $ this-> kvdb-> get ($ this-> key ); return intval ($ num) ;}} class CKvdb // kvdb operations encapsulated in this class. {Private $ db; function _ construct () {$ this-> db = new SaeKv (); $ this-> db-> init ();} public function set ($ key, $ value) {$ this-> db-> set ($ key, $ value);} public function get ($ key) {return $ this-> db-> get ($ key );}}

Add Counting Code

On the content page of your article, add the following Ajax Request Code, which is based on jQuery:

var keyTemp = $('#postTemp').text();$.get('http://localhost/counter.php',{ action:'add',key:keyTemp });$.get('http://localhost/counter.php',{ action:'get',key:keyTemp },function(data){    $('#view').text(data+' Views');});

The keyTemp variable is the alias of the document, that is, the key stored in KVDB. I saved the code to a hidden div and then obtained the content of the div when using Ajax. In Ajax, the first get is to access counter. php counting class with parameters to add 1. the second get is to get the access value and put the obtained value in the corresponding place.

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.