Home >Backend Development >PHP Tutorial >php $_SERVER['HTTP_USER_AGENT'] usage introduction_PHP tutorial

php $_SERVER['HTTP_USER_AGENT'] usage introduction_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:362890browse

In php, HTTP_USER_AGENT is used to obtain user-related information, including the user's browser, operating system project, whether the alex toolbar is installed, and other information. Friends who need to know more can refer to it.


1. When a user accesses the server, use the field ['HTTP_USER_AGENT'] in the PHP super global variable $_SERVER array to obtain all the information of the accessing user

The code is as follows Copy code
 代码如下 复制代码

echo $_SERVER['HTTP_USER_AGENT'];
?>

echo $_SERVER['HTTP_USER_AGENT'];<🎜> ?>

Output results

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 above string and the user’s browser operating system information

3. PHP allocates the required CSS files, JS, etc. based on the matched string (user’s browser operating system information) and returns it to the user

My Browser Operating System Information
Browser: Chrome 5.0

Platform: Windows 7

The code is as follows Copy code

//Display the browser information of the visiting user
echo 'Browser: ' . determinebrowser($Agent) . '
';
//Display the operating system platform of the visiting user
echo 'Platform: ' . determineplatform($Agent). '
';

//Positive expression comparison and analysis of the string in $_SERVER['HTTP_USER_AGENT'] to obtain information about the visiting user's browser
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;
}

// 同理获取访问用户的浏览器的信息
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="Windows 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('NetBSD',$Agent)) {
$browserplatform="NetBSD";
}
elseif (eregi('BSD',$Agent)) {
$browserplatform="BSD";
}
elseif (ereg('OSF1',$Agent)) {
$browserplatform="OSF1";
}
elseif (ereg('IRIX',$Agent)) {
$browserplatform="IRIX";
}
elseif (eregi('FreeBSD',$Agent)) {
$browserplatform="FreeBSD";
}
if ($browserplatform=='') {$browserplatform = "Unknown"; }
return $browserplatform;
}

?>

判断浏览器语言的具体程序如下

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); //只取前4位,这样只判断最优先的语言。如果取前5位,可能出现en,zh的情况,影响判断。 
 代码如下
 代码如下 复制代码

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); //只取前4位,这样只判断最优先的语言。如果取前5位,可能出现en,zh的情况,影响判断。
if (preg_match("/zh-c/i", $lang))
echo "简体中文";
else if (preg_match("/zh/i", $lang))
echo "繁?中文";
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 "Japanese";
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"];
?>

复制代码

if (preg_match("/zh-c/i", $lang))  <🎜> echo "简体中文";  <🎜> else if (preg_match("/zh/i", $lang))  <🎜> echo "繁?中文";  <🎜> 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 "Japanese";  <🎜> 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"];  <🎜> ?> 判断浏览器类型主要是靠分析_SERVER["HTTP_USER_AGENT"]的内容,而分析浏览器语言则是分析_SERVER["HTTP_ACCEPT_LANGUAGE"]。

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628805.htmlTechArticleIn php, HTTP_USER_AGENT is used to obtain user-related information, including the user's browser, operating system project, and whether it is installed Here is the alex toolbar article, waiting for information, friends who need to know...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn