How PHP Gets the client browser and operating system information

Source: Internet
Author: User
Tags ereg
In a more intelligent program, PHP can obtain the client browser and operating system information, and then depending on the browser and system type, loading different pages to provide more personalized service.

Here we come together to learn how to use PHP to get client browser and operating system information, interested friends can refer to the next.

1, PHP Super global variable $_server array field [' Http_user_agent '] get access to all information of the user
The following is a string obtained by $_server[' Http_user_agent '):
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 (the user's browser operating system information), the allocation of the required CSS, JS, etc., returned to the user

My browser and operating system information:
Browser:chrome 5.0
Platform:windows 7

A complete example is given below.
<?php
Display the browser information of the Access user
Echo ' Browser: '. Determinebrowser ($Agent). ' <BR> ';
Show access to the user's operating system platform
Echo ' Platform: '. Determineplatform ($Agent). ' <BR> ';

Link:http://www.jbxue.com
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;
}
?>
  • 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.