-
- /**
- * Classes for obtaining visitor information: language, browser, operating system, IP, geographical location, ISP.
- * Date: 2013/10/11
- * Editor: bbs.it-home.org
- * Use:
- $obj = new class_guest_info;
- $obj->GetLang(); //Get guest language: Simplified Chinese, Traditional Chinese, English.
- $obj->GetBrowser(); //Get the visitor's browser: MSIE, Firefox, Chrome, Safari, Opera, Other.
- $obj->GetOS(); //Get the guest operating system: Windows, MAC, Linux, Unix, BSD, Other.
- $obj->GetIP(); //Get the visitor IP address.
- $obj->GetAdd(); //Get the visitor's geographical location and use Baidu hidden interface.
- $obj->GetIsp(); //Get the guest ISP, use Baidu hidden interface.
- */
- class class_guest_info{
- function GetLang() {
- $Lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4);
- // Use substr() to intercept the string, starting from 0, intercepting 4 characters
- if (preg_match('/zh-c/i',$Lang)) {
- //preg_match() regular expression matching function
- $Lang = 'Simplified Chinese';
- }
- elseif (preg_match('/zh/i',$Lang)) {
- $Lang = 'Traditional Chinese';
- }
- else {
- $Lang = 'English';
- }
- return $Lang;
- }
- function GetBrowser() {
- $Browser = $_SERVER['HTTP_USER_AGENT'];
- if (preg_match('/MSIE/i',$Browser)) {
- $Browser = 'MSIE';
- }
- elseif (preg_match('/Firefox/i',$Browser)) {
- $Browser = 'Firefox';
- }
- elseif (preg_match('/Chrome/i',$Browser)) {
- $Browser = ' Chrome';
- }
- elseif (preg_match('/Safari/i',$Browser)) {
- $Browser = 'Safari';
- }
- elseif (preg_match('/Opera/i',$Browser)) {
- $Browser = 'Opera';
- }
- else {
- $Browser = 'Other';
- }
- return $Browser;
- }
- function GetOS() {
- $OS = $_SERVER['HTTP_USER_AGENT'];
- if ( preg_match('/win/i',$OS)) {
- $OS = 'Windows';
- }
- elseif (preg_match('/mac/i',$OS)) {
- $OS = 'MAC';
- }
- elseif (preg_match('/linux/i',$OS)) {
- $OS = 'Linux';
- }
- elseif (preg_match('/unix/i',$OS)) {
- $OS = ' Unix';
- }
- elseif (preg_match('/bsd/i',$OS)) {
- $OS = 'BSD';
- }
- else {
- $OS = 'Other';
- }
- return $OS;
- }
- function GetIP() {
- if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
- //If the variable is a non-empty or non-zero value, empty() returns FALSE.
- $IP = explode(',',$_SERVER['HTTP_CLIENT_IP']);
- }
- elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $IP = explode(',',$_SERVER[ 'HTTP_X_FORWARDED_FOR']);
- }
- elseif (!empty($_SERVER['REMOTE_ADDR'])) {
- $IP = explode(',',$_SERVER['REMOTE_ADDR']);
- }
- else {
- $ IP[0] = 'None';
- }
- return $IP[0];
- }
- private function GetAddIsp() {
- $IP = $this->GetIP();
- $AddIsp = mb_convert_encoding(file_get_contents(' http://open.baidu.com/ipsearch/s?tn=ipjson&wd='.$IP),'UTF-8','GBK');
- //mb_convert_encoding() Convert character encoding.
- if (preg_match('/noresult/i',$AddIsp)) {
- $AddIsp = 'None';
- }
- else {
- $Sta = stripos($AddIsp,$IP) + strlen($IP) + strlen ('from');
- $Len = stripos($AddIsp,'"}')-$Sta;
- $AddIsp = substr($AddIsp,$Sta,$Len);
- }
- $AddIsp = explode(' ' ,$AddIsp);
- return $AddIsp;
- }
- function GetAdd() {
- $Add = $this->GetAddIsp();
- return $Add[0];
- }
- function GetIsp() {
- $Isp = $this->GetAddIsp();
- if ($Isp[0] != 'None' && isset($Isp[1])) {
- $Isp = $Isp[1];
- }
- else {
- $Isp = 'None';
- }
- return $Isp;
- }
- }
- ?>
Copy code
>>>Articles you may be interested in:
PHP Sina interface to query IP geographical location
php Tencent IP sharing plan to obtain IP geographical location
php gets geographical location by IP
PHP gets the geolocation code via IP
Code sharing for php to obtain website location and operating system information
An example reference for php to obtain geographical location through IP
php implementation code to obtain the user’s real IP and geographical location (Taobao IP interface)
php gets current geographical location interface based on IP address
|