Understanding $_SERVER['HTTP_USER_AGENT']
$_SERVER['HTTP_USER_AGENT']
is a server-side variable used to identify the client's browser from the HTTP request header.
It contains a string with information about the client’s browser name, version, operating system, device type, crawling bots, and installed plugins.
- Useful for checking which browser the client is using.
- As a PHP superglobal variable, it can be accessed from the global scope.
Caution!
Client-side information such as $_SERVER['HTTP_USER_AGENT']
should not be solely relied upon for security purposes and requires additional proper safeguards when used in security-related contexts.
Caution!
Depending on caching settings, the value of $_SERVER['HTTP_USER_AGENT']
may be cached. Cached values might not reflect the actual client information, which can cause issues when using $_SERVER['HTTP_USER_AGENT']
to determine client characteristics or deliver personalized content.
$_SERVER['HTTP_USER_AGENT']
Variable Values
To check the value of the $_SERVER['HTTP_USER_AGENT']
variable, you can use the following:
echo $_SERVER['HTTP_USER_AGENT'];
/* You can use any method to inspect the variable,
such as echo or var_dump($_SERVER['HTTP_USER_AGENT']); */
Assuming the sample output of this script is as follows:
$_SERVER['HTTP_USER_AGENT']
variable, showing only partial information for clarity
From the sample output above, you can extract the following information:
- Operating System: Windows NT 10.0
- Platform: Win64 (64-bit Windows)
- Rendering Engine: WebKit (Apple's WebKit engine, version 537.36)
- Browser Name: Chrome
- Browser Version: 119.0.0.0
As demonstrated, $_SERVER['HTTP_USER_AGENT']
contains a string with information about the client’s browser name, version, operating system, device, and installed plugins.
Practical Uses of $_SERVER['HTTP_USER_AGENT']
Values
The value of $_SERVER['HTTP_USER_AGENT']
can be very useful in the following situations:
- Checking the client’s browser
- Detecting mobile devices in PHP
- Differentiating between iOS and Android devices in PHP
Checking the Client's Browser
$_SERVER['HTTP_USER_AGENT']
is useful for checking (identifying) which browser the client is using. Here, we will show how to detect a few major browsers.
Note:
The example codes provided here demonstrate simple methods for detecting the client’s browser based on the User Agent string. Browser detection using the User Agent is not perfect, and additional safeguards are necessary if used for security-related purposes.
Checking for Chrome Browser
if (stripos($_SERVER['HTTP_USER_AGENT'], 'chrome') !== false) {
echo 'The visitor is using the Chrome browser.';
}
Code explanation:
$_SERVER['HTTP_USER_AGENT']
– represents the User Agent string of the client making the current HTTP request.stripos($_SERVER['HTTP_USER_AGENT'], 'chrome')
– uses thestripos()
function to check if the User Agent string contains the word 'chrome'.stripos()
is case-insensitive and returns the position of the match if found.!== false
– the condition evaluates to true if 'chrome' is found.echo 'The visitor is using the Chrome browser.'
– prints this message if the user is using Chrome.
Thus, this code outputs a message when the client's browser is Chrome.
Checking for Safari Browser
$ua = $_SERVER['HTTP_USER_AGENT'];
if ((stripos($ua, 'safari') !== false) && (stripos($ua, 'chrome') === false)) {
echo 'The visitor is using the Safari browser.';
}
In the User Agent string, Safari browsers include the word 'Safari'.
Note that Chrome browsers also include the word 'Safari' in their User Agent string. Therefore, the example code above checks for 'Safari' only when 'Chrome' is not present, ensuring that the visitor is actually using Safari.
Checking for Firefox Browser
if (stripos($_SERVER['HTTP_USER_AGENT'], 'firefox') !== false) {
echo 'The visitor is using the Firefox browser.';
}