Current Location: Home> Latest Articles> Use stripos to distinguish user agents (User-Agent)

Use stripos to distinguish user agents (User-Agent)

M66 2025-05-31

When developing web applications, understanding different user agents (User-Agents) can help you optimize your website experience, customize responses, and better handle different devices and browsers. The stripos() function in PHP is a very useful tool that can be used to detect certain specific information in user agent strings. This article will show how to use the stripos() function to distinguish different user agents.

What is the scripos() function?

stripos() is a built-in function in PHP. It is used to find the location where a string first appears in another string, ignoring case. If the specified substring is found, the position of the substring in the parent string is returned, and if not found, false is returned.

Function prototype:

 stripos(string $haystack, string $needle, int $offset = 0): int|false
  • $haystack : The target string (i.e. the string being searched for).

  • $needle : The substring to be found.

  • $offset : Optional parameter, specifying where to start the search.

How to use stripos() to distinguish different user agents?

User-Agent is a string sent by a browser or device to a web server. It contains information about the browser, operating system, and device. By analyzing the user agent string, we can identify which browsers, operating systems, and device types are being used by the visitors.

Assuming we want to distinguish different browsers, we can use the following steps:

  1. Gets the user agent string.

  2. Use stripos() to find out if a specific browser identifier is included.

Here is an example showing how to use stripos() to identify a visitor's browser.

 <?php
// Get user agent string
$userAgent = $_SERVER['HTTP_USER_AGENT'];

// Check if it is Chrome Browser
if (stripos($userAgent, 'Chrome') !== false) {
    echo "You are using Chrome Browser";
}
// Check if it is Firefox Browser
elseif (stripos($userAgent, 'Firefox') !== false) {
    echo "You are using Firefox Browser";
}
// Check if it is Safari Browser
elseif (stripos($userAgent, 'Safari') !== false) {
    echo "You are using Safari Browser";
}
// Check if it is Edge Browser
elseif (stripos($userAgent, 'Edge') !== false) {
    echo "You are using Edge Browser";
} else {
    echo "无法识别您的Browser";
}
?>

How to distinguish between different operating systems?

In addition to the browser, the user agent string also contains the operating system information. We can also use stripos() to determine whether the visitor is using Windows, Mac, Linux, or mobile operating systems (such as Android and iOS).

 <?php
// Get user agent string
$userAgent = $_SERVER['HTTP_USER_AGENT'];

// Check if it is Windows operating system
if (stripos($userAgent, 'Windows NT') !== false) {
    echo "You are using Windows operating system";
}
// Check if it is Mac operating system
elseif (stripos($userAgent, 'Mac OS X') !== false) {
    echo "You are using Mac operating system";
}
// Check if it is Linux operating system
elseif (stripos($userAgent, 'Linux') !== false) {
    echo "You are using Linux operating system";
}
// Check if it is iOS operating system
elseif (stripos($userAgent, 'iPhone') !== false || stripos($userAgent, 'iPad') !== false) {
    echo "You are using iOS operating system";
}
// Check if it is Android operating system
elseif (stripos($userAgent, 'Android') !== false) {
    echo "You are using Android operating system";
} else {
    echo "无法识别您的operating system";
}
?>

Use stripos() to distinguish device types

By analyzing the user agent, it is also possible to distinguish whether the visitor is using a desktop or a mobile device. User agents for mobile devices will usually contain words like "Mobile" or "iPhone", while user agents for desktop devices will not. Here is an example code for how to determine the type of device:

 <?php
// Get user agent string
$userAgent = $_SERVER['HTTP_USER_AGENT'];

// Determine whether it is a mobile device
if (stripos($userAgent, 'Mobile') !== false || stripos($userAgent, 'Android') !== false || stripos($userAgent, 'iPhone') !== false) {
    echo "You are using移动设备";
} else {
    echo "You are using桌面设备";
}
?>

Summarize

By using PHP's stripos() function, we can easily extract information from user agent strings to identify browser, operating system, and device types. This is essential for customizing user experience and optimizing web response.