This is a website traffic statistics instance based on php and mysql. You can collect statistics on the sources, search engines, ip addresses, and Keywords of website visitors. I hope this instance will help you.
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 ); } |