PHP language, browser, operating system, IP address, geographic location, ISP

Source: Internet
Author: User
: This article describes the PHP language, browser, operating system, IP address, geographic location, and ISP. if you are interested in the PHP Tutorial, refer to it.
 $ Obj = new class_guest_info;
$ Obj-> GetLang (); // Obtain the visitor's language: Simplified Chinese, Traditional Chinese, and English.
$ Obj-> GetBrowser (); // Obtain the visitor's browsers: MSIE, Firefox, Chrome, Safari, Opera, and Other.
$ Obj-> GetOS (); // Obtain guest operating systems: Windows, MAC, Linux, Unix, BSD, and Other.
$ Obj-> GetIP (); // Obtain the visitor's IP address.
$ Obj-> GetAdd (); // Obtain the geographical location of a visitor. use the Baidu hidden interface.
$ Obj-> GetIsp (); // Obtain the visitor's ISP and use the Baidu hidden interface.
Class class_guest_info
{
Function GetLang ()
{
$ Lang = substr ($ _ SERVER ['http _ ACCEPT_LANGUAGE '], 0, 4 );
// Use substr () to intercept a string, starting from 0 to 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 = 'msi ';
} Elseif (preg_match ('/Firefox/I', $ Browser )){
$ Browser = 'Firefox ';
} Elseif (preg_match ('/Chrome/I', $ Browser )){
$ Browser = 'chromi ';
} 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 (! Emptyempty ($ _ SERVER ['http _ CLIENT_IP ']) {
// If the variable is not null or a non-zero value, empty () returns FALSE.
$ IP = explode (',', $ _ SERVER ['http _ CLIENT_IP ']);
} Elseif (! Emptyempty ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {
$ IP = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']);
} Elseif (! Emptyempty ($ _ 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/stn?ipjson&wd='. $ IP), 'utf-8', 'gbk ');
// Mb_convert_encoding () converts character encoding.
If (preg_match ('/noresult/I', $ AddIsp )){
$ AddIsp = 'none ';
} Else {
$ Sta = stripos ($ AddIsp, $ 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;
}
}

The above describes the PHP language, browser, operating system, IP address, geographic location, ISP, and other content. I hope to help anyone who is interested in the PHP Tutorial.

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.