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

Source: Internet
Author: User
Tags ereg server array

In php, HTTP_USER_AGENT is used to obtain user information, including the user's browser, operating system engineering, and whether the alex toolbar is installed. For more information, see.


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"].

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.