This article describes the AJAX implementation of static pages of the article Access statistics function. Share to everyone for your reference, specific as follows:
As we all know, static page is not only fast, but also to SEO has certain help. Some days ago, wrote a post about "in the SAE platform to achieve WordPress page pure static to Kvdb." After using myself for some time, I found that the speed was really obvious. But then one of the problems is that, because the article static, the page does not go through the WordPress program processing, which led to the article's access statistics ineffective. Of course, there is a plug-in called Wp-postview can solve this problem, but I do not like plug-ins, because it will slow down the overall speed. So here's a solution, which is to use Ajax to implement statistics, also based on the SAE platform.
Define article Access statistics class
This is actually in front of me in the post has mentioned that the Kvdb+taskqueue implementation of high-performance counters, this can be simply modified. Because it is not processed through PHP, the queue service is not available to count. Also define the Count class, and put it under the site root directory:
$countkey =$_get[' key '];//get the Count key if ($countkey = "") exit to be manipulated;
if ($_get[' action ']== "add") {$cou =new counter ($countkey);
$cou->inc ();//Count key corresponds to the value plus 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;
The public function Dec () {$num = $this->kvdb->get ($this->key)-1;
$this->kvdb->set ($this->key, $num);
return $num;
The public function get () {$num = $this->kvdb->get ($this->key);
Return Intval ($num);
} class Ckvdb//This class encapsulates the KVDB operation.
{Private $db;
function __construct () {$this->db=new saekv ();
$this->db->init ();
The public function set ($key, $value) {$this->db->set ($key, $value); } PubLic function Get ($key) {return $this->db->get ($key);
}
}
Add Count Code
In your article content page, 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 article, which is the key that is stored in the KVDB. I put the Jiancun into a hidden div and then get the contents of the DIV when I use Ajax. The first get in Ajax is to access the counter.php count class, and with the parameters, to achieve access plus 1. The second get is the access value, and the value is taken to the appropriate place.
For more information about PHP interested readers can view the site topics: "Php+ajax Tips and Applications Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary ( including word,excel,access,ppt), the "Summary of PHP date and time usage", "PHP Introduction to Object-oriented Programming", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and " A summary of common PHP database operations tips
I hope this article will help you with the PHP program design.