ThinkPHP + ajax: how to calculate page pv views, thinkphpajax

Source: Internet
Author: User

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 ');}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.