PHP records visitor's browsing information, and php records Visitor Information

Source: Internet
Author: User

PHP records visitor's browsing information, and php records Visitor Information

It records the geographical location, operating system, browser, IP address, time, and accessed file of the visitor.

1. First create a comm_function.php file:

<? Php // echo $ _ SERVER ['HTTP _ USER_AGENT ']; // echo "<br/> ". $ _ SERVER ['remote _ ADDR '];/*** obtain the client type, mobile phone number, computer type, and operating system type. ** @ Param string $ subject */function get_ OS ($ agent) {$ OS = false; if (preg_match ('/win/I', $ agent) & strpos ($ agent, '95 ') {$ OS = 'windows 95';} else if (preg_match ('/win 9x/I', $ agent) & strpos ($ agent, '4. 90 ') {$ OS = 'windows Me';} else if (preg_match ('/win/I ', $ agent) & preg_match ('/98/I ', $ agent) {$ OS = 'windows 98 ';} else if (preg_match ('/win/I ', $ agent) & pr Eg_match ('/nt 6.0/I', $ agent) {$ OS = 'windows Vista ';} else if (preg_match ('/win/I ', $ agent) & preg_match ('/nt 6.1/I', $ agent) {$ OS = 'windows 7';} else if (preg_match ('/win/I ', $ agent) & preg_match ('/nt 6.2/I', $ agent) {$ OS = 'windows 8 ';} else if (preg_match ('/win/I', $ agent) & preg_match ('/nt 10.0/I', $ agent) {$ OS = 'windows 10 '; // Add win10 judgment} else if (preg_m Atch ('/win/I', $ agent) & preg_match ('/nt 5.1/I', $ agent) {$ OS = 'windows XP ';} else if (preg_match ('/win/I', $ agent) & preg_match ('/nt 5/I', $ agent )) {$ OS = 'windows 2000 ';} else if (preg_match ('/win/I ', $ agent) & preg_match ('/nt/I ', $ agent )) {$ OS = 'windows NT ';} else if (preg_match ('/win/I ', $ agent) & preg_match ('/32/I ', $ agent )) {$ OS = 'windows 32';} else if (Preg_match ('/linux/I', $ agent) {if (preg_match ("/Mobile/", $ agent )) {if (preg_match ("/QQ/I", $ agent) {$ OS = "Android QQ Browser" ;}else {$ OS = "Android Browser ";}} else {$ OS = 'pc-Linux ';} else if (preg_match ('/Mac/I ', $ agent) {if (preg_match ("/Mobile /", $ agent) {if (preg_match ("/QQ/I", $ agent) {$ OS = "IPhone QQ Browser ";} else {$ OS = "IPhone Browser" ;}} else {$ OS = 'mac OS x ';}} Else if (preg_match ('/unix/I', $ agent) {$ OS = 'unix ';} else if (preg_match ('/sun/I ', $ agent) & preg_match ('/OS/I', $ agent) {$ OS = 'sunos ';} else if (preg_match ('/ibm/I ', $ agent) & preg_match ('/OS/I', $ agent) {$ OS = 'ibm OS/2';} else if (preg_match ('/Mac/I ', $ agent) & preg_match ('/PC/I', $ agent) {$ OS = 'macintosh ';} else if (preg_match ('/PowerPC/I ', $ agent )) {$ OS = 'powerpc';} else if (preg_match ('/AIX/I', $ agent) {$ OS = 'Aix ';} else if (preg_match ('/HPUX/I', $ agent) {$ OS = 'hpux ';} else if (preg_match ('/NetBSD/I ', $ agent) {$ OS = 'netsd';} else if (preg_match ('/BSD/I', $ agent) {$ OS = 'bsd ';} else if (preg_match ('/OSF1/I', $ agent) {$ OS = 'oss1';} else if (preg_match ('/IRIX/I ', $ agent) {$ OS = 'irix ';} else if (preg _ Match ('/FreeBSD/I', $ agent) {$ OS = 'freebsd ';} else if (preg_match ('/teleport/I ', $ agent )) {$ OS = 'teleport ';} else if (preg_match ('/flashget/I ', $ agent) {$ OS = 'flashget ';} else if (preg_match ('/webzip/I', $ agent) {$ OS = 'webzip ';} else if (preg_match ('/offline/I ', $ agent) {$ OS = 'offline';} else {$ OS = 'unknown OS ';} return $ OS ;} /*** obtain the browser type of the client * @ return string */f Unction get_broswer ($ sys) {if (stripos ($ sys, "Firefox/")> 0) {preg_match ("/Firefox \/([^;)] +) +/I ", $ sys, $ B); $ exp [0] =" Firefox "; $ exp [1] = $ B [1]; // get firefox version} elseif (stripos ($ sys, "Maxthon")> 0) {preg_match ("/Maxthon \/([\ d \.] +)/", $ sys, $ aoyou); $ exp [0] =" aoyou "; $ exp [1] = $ aoyou [1];} elseif (stripos ($ sys, "MSIE")> 0) {preg_match ("/MSIE \ s + ([^;)] +) +/I", $ sys, $ ie); $ exp [0] = "IE"; $ exp [1] = $ Ie [1]; // get the IE version} elseif (stripos ($ sys, "OPR")> 0) {preg_match ("/OPR \/([\ d \.] +)/", $ sys, $ opera); $ exp [0] =" Opera "; $ exp [1] = $ opera [1];} elseif (stripos ($ sys, "Edge")> 0) {// The chrome kernel tag added to the win10 Edge browser matches preg_match ("/Edge \/([\ d \.] +)/", $ sys, $ Edge); $ exp [0] =" Edge "; $ exp [1] = $ Edge [1];} elseif (stripos ($ sys, "Chrome")> 0) {preg_match ("/Chrome \/([\ d \.] +)/", $ sys, $ google ); $ Exp [0] = "Chrome"; $ exp [1] = $ google [1]; // get google chrome version} elseif (stripos ($ sys, 'rv: ')> 0 & stripos ($ sys, 'gecko')> 0) {preg_match ("/rv :( [\ d \.] +)/", $ sys, $ IE); $ exp [0] =" IE "; $ exp [1] = $ IE [1];} else {$ exp [0] = "unknown Browser"; $ exp [1] = "";} return $ exp [0]. '('. $ exp [1]. ')';}/*** obtain the location information of the Client IP Address * @ param unknown $ ip * @ return string */function get_address_by_ip ($ ip) {$ url = "http://ip.taob Ao.com/service/getIpInfo.php? Ip = ". $ ip; $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_HEADER, 0); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); $ info = curl_exec ($ curl); curl_close ($ curl); return $ info;} function clientlog () {$ useragent = $ _ SERVER ['HTTP _ USER_AGENT ']; $ clientip = $ _ SERVER ['remote _ ADDR']; $ client_info = get_ OS ($ useragent ). "---". get_broswer ($ useragent); $ rawdat A_position = get_address_by_ip ($ clientip); $ rawdata_position = json_decode ($ rawdata_position, true); $ country = $ rawdata_position ['data'] ['country']; $ province = $ rawdata_position ['data'] ['region']; $ city = $ rawdata_position ['data'] ['city']; $ nettype = $ rawdata_position ['data'] ['isp ']; $ time = date ('Y-m-d h: m: s '); $ data = "client from {$ country} {$ province} {$ city} {$ nettype}: {$ client_info}, IP: {$ Clientip}. The {$ _ SERVER ['php _ SELF ']} file was accessed at {$ time! \ R \ n "; $ filename ="./log. log "; if (! File_exists ($ filename) {fopen ($ filename, "w +");} file_put_contents ($ filename, $ data, FILE_APPEND );}

2. Introduce this comm_function.php file in another file.

require_once "comm_function.php"; 

The above PHP record the visitor's browsing information is all the content shared by the editor. I hope to give you a reference and support for the house of friends.

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.