Php $ _ SERVER ['http _ USER_AGENT '] usage

Source: Internet
Author: User
Tags ereg server array


1. When a user accesses the SERVER, use the PHP Super global variable $ _ SERVER array field ['http _ USER_AGENT '] to obtain all information of the user

The code is as follows: Copy code

<? Php
Echo $ _ SERVER ['http _ USER_AGENT '];
?>

Output result

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;. net clr 2.0.50727; InfoPath.2; AskTbPTV/5.17.0.25589; Alexa Toolbar)

2. Use regular expressions to match the preceding strings and users' browser operating system information

3. PHP allocates the necessary CSS files, JS files, and so on based on the matched string (the user's browser operating system information... Return to User

My browser operating system information
Browser: Google Chrome 5.0

Platform: Windows 7

The code is as follows: Copy code

<? Php

// Display the browser information of the visitor
Echo 'Browser: '. determinebrowser ($ Agent).' <BR> ';
// Display the operating system platform used to access the user
Echo 'platform: '. determineplatform ($ Agent).' <BR> ';

// Parse a positive expression against the string in $ _ SERVER ['http _ USER_AGENT '] to obtain information about the browser used to access the user
Function determinebrowser ($ Agent ){
$ Browseragent = ""; // browser
$ Browserversion = ""; // browser version
If (ereg ('msie ([0-9]. [0-9] {1, 2}) ', $ Agent, $ version )){
$ Browserversion = $ version [1];
$ Browseragent = "Internet Explorer ";
} Else if (ereg ('Opera/([0-9] {1, 2}. [0-9] {1, 2}) ', $ Agent, $ version )){
$ Browserversion = $ version [1];
$ Browseragent = "Opera ";
} Else if (ereg ('Firefox/([0-9.] {1, 5}) ', $ Agent, $ version )){
$ Browserversion = $ version [1];
$ Browseragent = "Firefox ";
} Else if (ereg ('Chrome/([0-9.] {1, 3}) ', $ Agent, $ version )){
$ Browserversion = $ version [1];
$ Browseragent = "Chrome ";
}
Else if (ereg ('Safari/([0-9.] {1, 3}) ', $ Agent, $ version )){
$ Browseragent = "Safari ";
$ Browserversion = "";
}
Else {
$ Browserversion = "";
$ Browseragent = "Unknown ";
}
Return $ browseragent. "". $ browserversion;
}

// Obtain the information of the browser used to access the user.
Function determineplatform ($ Agent ){
$ Browserplatform = '';
If (eregi ('win', $ Agent) & strpos ($ Agent, '95 ')){
$ Browserplatform = "Windows 95 ";
}
Elseif (eregi ('win 9x', $ Agent) & strpos ($ Agent, '4. 90 ')){
$ Browserplatform = "Windows ME ";
}
Elseif (eregi ('win', $ Agent) & ereg ('98 ', $ Agent )){
$ Browserplatform = "Windows 98 ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt 5.0 ', $ Agent )){
$ Browserplatform = "window 2000 ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt 5.1 ', $ Agent )){
$ Browserplatform = "Windows XP ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt 6.0 ', $ Agent )){
$ Browserplatform = "Windows Vista ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt 6.1 ', $ Agent )){
$ Browserplatform = "Windows 7 ";
}
Elseif (eregi ('win', $ Agent) & ereg ('32', $ Agent )){
$ Browserplatform = "Windows 32 ";
}
Elseif (eregi ('win', $ Agent) & eregi ('nt ', $ Agent )){
$ Browserplatform = "Windows NT ";
} Elseif (eregi ('Mac OS ', $ Agent )){
$ Browserplatform = "Mac OS ";
}
Elseif (eregi ('Linux ', $ Agent )){
$ Browserplatform = "Linux ";
}
Elseif (eregi ('unix ', $ Agent )){
$ Browserplatform = "Unix ";
}
Elseif (eregi ('sun', $ Agent) & eregi ('OS', $ Agent )){
$ Browserplatform = "SunOS ";
}
Elseif (eregi ('IBM ', $ Agent) & eregi (' OS ', $ Agent )){
$ Browserplatform = "ibm OS/2 ";
}
Elseif (eregi ('Mac', $ Agent) & eregi ('PC', $ Agent )){
$ Browserplatform = "Macintosh ";
}
Elseif (eregi ('powerpc', $ Agent )){
$ Browserplatform = "PowerPC ";
}
Elseif (eregi ('aix ', $ Agent )){
$ Browserplatform = "AIX ";
}
Elseif (eregi ('hpux ', $ Agent )){
$ Browserplatform = "HPUX ";
}
Elseif (eregi ('netsd', $ Agent )){
$ Browserplatform = "NetBSD ";
}
Elseif (eregi ('bsd', $ Agent )){
$ Browserplatform = "BSD ";
}
Elseif (ereg ('oss1', $ Agent )){
$ Browserplatform = "OSF1 ";
}
Elseif (ereg ('irix ', $ Agent )){
$ Browserplatform = "IRIX ";
}
Elseif (eregi ('freebsd ', $ Agent )){
$ Browserplatform = "FreeBSD ";
}
If ($ browserplatform = '') {$ browserplatform =" Unknown ";}
Return $ browserplatform;
}

?>

The specific procedure for determining the browser language is as follows:

The code is as follows: Copy code

<? Php
$ Lang = substr ($ _ SERVER ['http _ ACCEPT_LANGUAGE '], 0, 4); // only the first four digits are used to determine the preferred language. If the first five digits are obtained, en and zh may occur, affecting the judgment.
If (preg_match ("/zh-c/I", $ lang ))
Echo "simplified Chinese ";
Else if (preg_match ("/zh/I", $ lang ))
Echo "fan? Chinese ";
Else if (preg_match ("/en/I", $ lang ))
Echo "English ";
Else if (preg_match ("/fr/I", $ lang ))
Echo "French ";
Else if (preg_match ("/de/I", $ lang ))
Echo "German ";
Else if (preg_match ("/jp/I", $ lang ))
Echo "Japan ";
Else if (preg_match ("/ko/I", $ lang ))
Echo "Korean ";
Else if (preg_match ("/es/I", $ lang ))
Echo "Spanish ";
Else if (preg_match ("/sv/I", $ lang ))
Echo "Swedish ";
Else echo $ _ SERVER ["HTTP_ACCEPT_LANGUAGE"];
?>

To determine the browser type, analyze the content of _ SERVER ["HTTP_USER_AGENT"]. To analyze the browser language, analyze _ SERVER ["HTTP_ACCEPT_LANGUAGE"].

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.