Home Backend Development PHP Tutorial How to obtain client browser and operating system information in php

How to obtain client browser and operating system information in php

Nov 22, 2016 am 11:45 AM
Browser

In a more intelligent program, PHP can obtain the client browser and operating system information, and then load different pages according to the browser and system type to provide more personalized services.

Let’s learn how to use php to obtain client browser and operating system information. Friends who are interested can refer to it.

1. The field ['HTTP_USER_AGENT'] in the PHP super global variable $_SERVER array obtains all the information of the accessing user
The following is a string obtained through $_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. Use regular expressions to match the above string and the user's browser and operating system information.

3. PHP allocates the required CSS, JS, etc. based on the matched string (user’s browser operating system information), and returns it to the user

My browser and operating system information:
Browser: Chrome 5.0
Platform : Windows 7

A complete example is given below.
//Display the browser information of the visiting user
echo 'Browser: ' . determinebrowser($Agent) . '
';
//Display the operating system platform of the visiting user
echo 'Platform: ' . determineplatform($Agent). '
';

//link: http://www.jbxue.com
//Positive expression comparison and analysis of characters in $_SERVER['HTTP_USER_AGENT'] String to obtain the information of the visiting 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;
}

// Obtain access in the same way 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('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; 

?> 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to correctly display the locally installed 'Jingnan Mai Round Body' on the web page? How to correctly display the locally installed 'Jingnan Mai Round Body' on the web page? Apr 05, 2025 pm 10:33 PM

Using locally installed font files in web pages Recently, I downloaded a free font from the internet and successfully installed it into my system. Now...

How to control the top and end of pages in browser printing settings through JavaScript or CSS? How to control the top and end of pages in browser printing settings through JavaScript or CSS? Apr 05, 2025 pm 10:39 PM

How to use JavaScript or CSS to control the top and end of the page in the browser's printing settings. In the browser's printing settings, there is an option to control whether the display is...

Why does negative margins not take effect in some cases? How to solve this problem? Why does negative margins not take effect in some cases? How to solve this problem? Apr 05, 2025 pm 10:18 PM

Why do negative margins not take effect in some cases? During programming, negative margins in CSS (negative...

How to use locally installed font files on web pages? How to use locally installed font files on web pages? Apr 05, 2025 pm 10:57 PM

How to use locally installed font files on web pages Have you encountered this situation in web page development: you have installed a font on your computer...

The text under Flex layout is omitted but the container is opened? How to solve it? The text under Flex layout is omitted but the container is opened? How to solve it? Apr 05, 2025 pm 11:00 PM

The problem of container opening due to excessive omission of text under Flex layout and solutions are used...

Why does a specific div element in the Edge browser not display? How to solve this problem? Why does a specific div element in the Edge browser not display? How to solve this problem? Apr 05, 2025 pm 08:21 PM

How to solve the display problem caused by user agent style sheets? When using the Edge browser, a div element in the project cannot be displayed. After checking, I posted...

How to achieve horizontal scrolling effect of horizontal options by rotating elements in CSS? How to achieve horizontal scrolling effect of horizontal options by rotating elements in CSS? Apr 05, 2025 pm 10:51 PM

How to achieve horizontal scrolling effect of horizontal options in CSS? In modern web design, how to achieve a horizontal tab-like effect and support the mouse...

How to correctly select and set the element style with the first class name 'red' in CSS? How to correctly select and set the element style with the first class name 'red' in CSS? Apr 05, 2025 pm 08:24 PM

How to select and set the element style for the first specific class? In web development, it is often necessary to style elements of specific class names, especially when...

See all articles