ThinkPHP + ajax: how to calculate page pv views, thinkphpajax
This article describes how thinkPHP + ajax counts page pv views. We will share this with you for your reference. The details are as follows:
Statistics on pv volume are often used. The following code is implemented using ajax. Using ajax can avoid the impact of page cache. As long as the client's js code is executed, you can count the traffic.
There are two steps in total:
1. Place the following code on the html page for statistics. During the test, replace the address with your own address.
<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 on the corresponding controller. M ('table name') setInc ('field') is replaced with its own, that is, the method requested by the above ajax code.
// Ajax sets the click volume public function set_hits () {if (! $ _ GET ['id']) {return;} M ('Article')-> where ("id = '{$ _ GET ['id']}'") -> setInc ('hits ');}