PHP $_server[' http_user_agent ' Usage introduction _php tutorial

Source: Internet
Author: User
In PHP http_user_agent is used to obtain user-related information, including the user browser, the operation of systems engineering, whether the installation of Alex Toolbar Oh, and other information, there is a need to understand the friends can refer to.


1. When the user accesses the server, use PHP's super global variable $_server array field [' Http_user_agent '] to get access to all of the user's information

The code is as follows Copy Code

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. Using regular expressions to match the above string, the user's browser operating system information

3.PHP according to the matching string (user's browser operating system information), the allocation of required CSS files, JS and so on ... Return to User

My browser's operating system information
Browser:chrome 5.0

Platform:windows 7

The code is as follows Copy Code

Display the browser information of the Access user
Echo ' Browser: '. Determinebrowser ($Agent). '
';
Show access to the user's operating system platform
Echo ' Platform: '. Determineplatform ($Agent). '
';

A positive value expression that is better than parsing the string in $_server[' Http_user_agent '] gets access to the user's browser information
function Determinebrowser ($Agent) {
$browseragent = ""; Browser
$browserversion = ""; Version of the browser
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;
}

Similarly get access to the user's browser information
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 (' + ', $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;
}

?>

The specific procedures for judging the browser language are as follows

The code is as follows Copy Code

$lang = substr ($_server[' http_accept_language '), 0, 4); Take only the first 4 digits, so that only the most preferred language is judged. If take the first 5 bits, may appear en,zh situation, influence judgment.
if (Preg_match ("/zh-c/i", $lang))
echo "Simplified Chinese";
else if (Preg_match ("/zh/i", $lang))
echo "Traditional Chinese";
else if (Preg_match ("/en/i", $lang))
echo "中文版";
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"];
?>

The browser type is determined mainly by analyzing the content of _server["Http_user_agent", while the analysis browser language is the analysis _server["Http_accept_language".

http://www.bkjia.com/PHPjc/628805.html www.bkjia.com true http://www.bkjia.com/PHPjc/628805.html techarticle in PHP http_user_agent is used to obtain user-related information, including the user browser, the operation of systems engineering, whether the installation of Alex Toolbar bar Oh, and other information, there is a need to know the friend can ...

  • 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.