PHP code to obtain user's browser and operating system information_PHP tutorial

WBOY
Release: 2016-07-21 15:16:35
Original
917 people have browsed 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

$_SERVER['HTTP_USER_AGENT'] string when I visited:

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. 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 codeThe code is as follows:

//Display visiting users Browser information
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 accessing 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;
}

//Similarly obtain the information of the visiting 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 (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 ( '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;
}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325880.htmlTechArticle1. When the user accesses the server, use the field ['HTTP_USER_AGENT'] in the PHP super global variable $_SERVER array to obtain $_SERVER['HTTP_USER_AGENT'] characters when accessing all the user's information...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template