PHP get the user's browser and operating system Information Code _php Tutorial

Source: Internet
Author: User
Tags ereg
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

$_server[' http_user_agent ' string when I visit:

mozilla/5.0 (Windows; U Windows NT 6.1; En-US) applewebkit/533.2 (khtml, like Gecko) chrome/5.0.342.3 safari/533.2

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 the Code code as follows:
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;
}

?>

http://www.bkjia.com/PHPjc/325880.html www.bkjia.com true http://www.bkjia.com/PHPjc/325880.html techarticle 1. When the user accesses the server, using PHP's super global variable $_server array field [' Http_user_agent '] gets access to all the user's information when I visit the $_server[' http_user_agent '] character ...

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