Home > Article > Backend Development > How to use thinkPHP+ajax to count page pv views
This article mainly introduces the method of thinkPHP+ajax to achieve statistical page pv views, involving thinkPHP template calling and database reading and writing related operation skills. Friends in need can refer to the following
The examples of this article describe thinkPHP +ajax method to implement statistical page pv views. Share it with everyone for your reference. The details are as follows:
Statistics of pv volume are very commonly used. The following code is implemented with ajax. Using ajax can avoid the impact of page caching, as long as the clientjs Once the code is executed, the traffic can be counted.
There are two steps in total:
1. Place the following code in the html page to be counted, and replace the address with your own when testing.
<script> var ajax; if(window.XMLHttpRequest){ ajax = new XMLHttpRequest();}else{ ajax = new ActiveXObject('Microsoft.XMLHTTP');} ajax.open('GET','MODULE/Article/set_hits/id/{$_GET['id']}',true); ajax.send(); </script>
2. The following code is placed in the corresponding controller, and M('table name') setInc('field') is replaced with Your own, which is the method requested by the ajax code above.
// ajax设置点击量 public function set_hits(){ if(!$_GET['id']){return;} M('article')->where("id = '{$_GET['id']}'")->setInc('hits'); }
The above is the detailed content of How to use thinkPHP+ajax to count page pv views. For more information, please follow other related articles on the PHP Chinese website!