Home  >  Article  >  Backend Development  >  PHP simple website traffic statistics program (source, search engine, IP, keywords)_PHP tutorial

PHP simple website traffic statistics program (source, search engine, IP, keywords)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:44:181210browse

This is an example of website traffic statistics based on php and mysql. It can count the source, search engine, IP, and keywords of people visiting the website. I hope this example will be helpful to all students.

Traffic statistics tools, such as cnzz, Baidu statistics, 51la, are all very good and have powerful functions. I have nothing to do, I wrote a simple traffic statistics tool, but it can only count access time, visitor IP, access source, visited pages, from seo/seo.html" target="_blank">search engines and searches Keywords. It is definitely trivial compared to professional traffic statistics work, but to put it bluntly, most of the collected data is not as difficult as we imagine. Sometimes it only requires a system variable or extracting some information from the URL. You can get the corresponding statistical information

.

(data table structure and some statistical information)

Program part:

The code is as follows Copy code


visited(); //Call statistical function (preferably placed on a public page)

//访问量
function visited(){
    $now_time = time();
    $referer_url = $_SERVER['HTTP_REFERER'];    //来自的页面地址
    $from_spider = $keywords = '';
    if(!empty($referer_url)){
        if(strstr($referer_url,'www.baidu.com')){
            $referer_url = explode("&",$referer_url);
            foreach($referer_url as $val){
                if(strstr($val,'wd=')){
                    $keywords = explode("wd=",$val);
                    $keywords = $keywords[1];
                }
            }
            $from_spider = '百度';
            $keywords = urldecode($keywords);
        }elseif(strstr($referer_url,'www.google')){
            $referer_url = explode("&",$referer_url);
            foreach($referer_url as $val){
                if(strstr($val,'q=')){
                    $keywords = explode("q=",$val);
                    $keywords = $keywords[1];
                }
            }
            $from_spider = '谷歌';
            $keywords = urldecode($keywords);
        }   
    }
    //只判断了百度和谷歌的,其他的原理一样
    $theData = array(
        'access_time'    => $now_time,
        'access_date'    => date("Y-m-d",$now_time),
        'access_url'    => $_SERVER['REQUEST_URI'],
        'referer_url'    => $_SERVER['HTTP_REFERER'],
        'keywords'        => $keywords,
        'ip_address'    => $_SERVER['REMOTE_ADDR'],
        'from_spider'    => $from_spider,
    );
    $sql = "insert into stats (access_time,access_date,access_url,referer_url,keywords,ip_address,from_spider) values ('".$theData['access_time']."','".$theData['access_date']."','".$theData['access_url']."','".$theData['referer_url']."','".$theData['keywords']."','".$theData['ip_address']."','".$theData['from_spider']."')";
    echo $sql; exit;mysql_query($sql);
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633108.htmlTechArticle这是一个基于php 与mysql的网站流量统计实例,可以统计出来过来访问网站人员的来源,搜索引擎,ip,关键词,希望此实例对各位同学会有所帮助...
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