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 A method to implement statistical page pv views. Share it with everyone for your reference. The details are as follows:
It is very commonly used to count pv volume. The following code is implemented with ajax. Using ajax can avoid the impact of page caching. As long as the client's js code is executed, statistics can be flow.
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') are replaced with your own. That 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 entire content of this article, thank you for reading.
Related recommendations:
Thinkphp AJAX dynamically verifies whether user input is legal
##
The above is the detailed content of thinkPHP+ajax method to realize statistics of page pv views. For more information, please follow other related articles on the PHP Chinese website!