Home  >  Article  >  Backend Development  >  thinkPHP+ajax method to realize statistics of page pv views

thinkPHP+ajax method to realize statistics of page pv views

不言
不言Original
2018-05-10 10:23:172029browse

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(&#39;Microsoft.XMLHTTP&#39;);}
ajax.open(&#39;GET&#39;,&#39;__MODULE__/Article/set_hits/id/{$_GET[&#39;id&#39;]}&#39;,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[&#39;id&#39;]){return;}
  M(&#39;article&#39;)->where("id = &#39;{$_GET[&#39;id&#39;]}&#39;")->setInc(&#39;hits&#39;);
}

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!

Statement:
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