PHP get guest IP, location and other skills to share

Source: Internet
Author: User
This article mainly and you introduced the use of PHP to obtain the visitor IP, location, browser and source pages and other information, the text gives a detailed sample code for everyone to reference the study, for everyone has a certain reference value, the need for friends below to see it together.

Sample code

<?php//this is similar to obtaining visitor information//convenient statistics class visitorinfo{//Get guest IP public Function getip () {$ip =false;  if (!empty ($_server["Http_client_ip")) {$ip = $_server["Http_client_ip"];   } if (!empty ($_server[' http_x_forwarded_for ')) {$ips = Explode (",", $_server[' http_x_forwarded_for ']);   if ($IP) {array_unshift ($ips, $ip); $ip = FALSE;}     for ($i = 0; $i < count ($ips); $i + +) {if (!eregi ("^ (10│172.16│192.168).", $ips [$i])) {$ip = $ips [$i];    Break }}} return ($ip? $ip: $_server[' remote_addr '); }//Get city, network operator and other information according to IP public function Findcitybyip ($ip) {$data = file_get_contents (' http://ip.taobao.com/service/  Getipinfo.php?ip= '. $ip); Return Json_decode ($data, $assoc =true);  }//Get user Browser type public function Getbrowser () {$agent =$_server["http_user_agent"];  if (Strpos ($agent, ' MSIE ')!==false | | strpos ($agent, ' rv:11.0 '))//ie11 judgment return "ie";  else if (Strpos ($agent, ' Firefox ')!==false) return "Firefox";  else if (Strpos ($agent, ' Chrome ')!==false) return "Chrome"; Else if (Strpos ($agent, ' opera ')!==false) return ' opera ';  else if ((Strpos ($agent, ' Chrome ') ==false) &&strpos ($agent, ' Safari ')!==false) return ' Safari '; else return ' unknown '; }//Get site Source Public Function GetFromPage () {return $_server[' http_referer '];}}
Related Article

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.