PHP to get the user's browser and operating system Information code _php Tips

Source: Internet
Author: User
Tags ereg

1. User access to the server, the use of PHP's super global variable $_server in the Array field [' Http_user_agent '] to get access to all the user 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), assign the required CSS files, JS and so on ... Return to the user

My browser's operating system information
Browser:chrome 5.0

Platform:windows 7

The code is as follows:

Copy Code code as follows:

<?php
Displays the browser information for the Access user
Echo ' Browser: '. Determinebrowser ($Agent). ' <BR> ';
Display the operating system platform for accessing users
Echo ' Platform: '. Determineplatform ($Agent). ' <BR> ';

The

//positive expression is more information
function Determinebrowser ($Agent) {
$ than the string in parsing $_server[' http_user_agent] gets access to the user's browser 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;
}

Similarly get information about accessing a user's browser
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 (' Win ', $Agent) && ereg (' Eregi ', $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 (' Win ', $Agent) && ereg (' Eregi ', $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;
}

?>

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.