Php simple website traffic statistics program (source, search engine, ip, keyword)

Source: Internet
Author: User
Tags foreach explode

Traffic Statistics tools, such as cnzz, Baidu Statistics, and 51, are both very good and powerful. I wrote a simple traffic statistics tool, but I can only count the access time, visitor ip address, access route, and access page, from seo/seo.html "target =" _ blank "> search engines and search keywords. Compared with professional traffic statistics, it must be insignificant. However, most of the statistics are not as difficult as we think, sometimes only one system variable or some information in the url can be extracted to obtain the corresponding statistical information.

 

(Data table structure and statistical data)

Procedure:

The code is as follows: Copy code


Visited (); // call the statistical function (preferably on the public page)

// Access Volume
Function visited (){
$ Now_time = time ();
$ Referer_url = $ _ SERVER ['http _ referer']; // address of the page from
$ 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 = 'Baidu ';
$ 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 = 'Google ';
$ Keywords = urldecode ($ keywords );
        }   
    }
// Only Baidu and Google are judged. The other principles are the same.
$ TheData = array (
'Access _ time' => $ now_time,
'Access _ date' => date ("Y-m-d", $ now_time ),
'Access _ url' => $ _ SERVER ['request _ url'],
'Referer _ url' = >$ _ SERVER ['http _ referer'],
'Keyword' => $ 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 ['keyword']. "','". $ theData ['IP _ address']. "','". $ theData ['from _ spider ']. "')";
Echo $ SQL; exit; mysql_query ($ SQL );
}

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.