How to use thinkPHP+ajax to count page pv views

黄舟
Release: 2023-03-06 17:08:01
Original
1690 people have browsed it

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(&#39;Microsoft.XMLHTTP&#39;);}
ajax.open(&#39;GET&#39;,&#39;MODULE/Article/set_hits/id/{$_GET[&#39;id&#39;]}&#39;,true);
ajax.send();
</script>
Copy after login

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[&#39;id&#39;]){return;}
  M(&#39;article&#39;)->where("id = &#39;{$_GET[&#39;id&#39;]}&#39;")->setInc(&#39;hits&#39;);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template