thinkphp implements statistics of page views

【Foreword】

   This article introduces thinkphp to count pageviews. The simple method adopted here does not filter ip

   Of course, some websites restrict IPs in order to prevent malicious page refreshes, and an IP can only be refreshed once a day. For the method of php to prevent malicious page refresh, please refer to https://blog.csdn.net/u011252402/article/details/47415101

 

 

【main body】

1. First, insert the pageview field shownum in the data table blog_article

alter table blog_article add column shownum varchar(40) default 0;

 

Second, the next two steps

(1) Put 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/index/id/{$_GET['id']}',true);
    ajax.send();
</script>

 

(2) The following code is placed in the corresponding controller, and M('table name') setInc('field') is replaced by its own, which is the method requested by the above code

public function index(){
    	$item = D('item')
                   ->field('t1.*,t2.catename as catename')
    		   ->alias('t1')
    	           ->join('left join blog_cate as t2 on t1.cateid=t2.id')
    	           ->where('t1.id = '.I('id'))
    	           ->find();
        //Views
        if(!$_GET['id']){return;}
        $shownum = M('article')->where("id = '{$_GET['id']}'")->setInc('shownum',1);
    	$this->assign('article',$article);
        $this->display();
    }

   When adding statistical code to the php code, if the static page caches the php code, the php code will not be executed. Using ajax can completely solve this problem.

 

【Summarize】

  The above can achieve a simple overlay of page views when clicking on the corresponding article or news on the list page.

 

 

 

 

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326097797&siteId=291194637