Current Location: Home> Latest Articles> Custom extension get_client_version supports more client information

Custom extension get_client_version supports more client information

gitbox 2025-05-11

When developing web applications, understanding the details of the client can help us perform more granular functional adaptation, security verification, and user behavior analysis. PHP is a flexible server-side language that can parse the client's User-Agent through functions, but we can also implement more powerful and extensible client information extraction functions through the customized get_client_version function.

1. Basic ideas

The core purpose of the get_client_version function is to extract the client's type (such as browser, operating system, device type) and version information from the request header. We will implement this by parsing $_SERVER['HTTP_USER_AGENT'] and support loading custom extension rules.

2. Function implementation

The following is the basic version of the get_client_version function implementation, and supports loading user-defined extensions:

 function get_client_version($user_agent = null, $custom_rules = []) {
    if (is_null($user_agent)) {
        $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
    }

    $clients = [
        'Chrome' => '/Chrome\/([0-9\.]+)/',
        'Firefox' => '/Firefox\/([0-9\.]+)/',
        'Safari' => '/Version\/([0-9\.]+).*Safari/',
        'Edge' => '/Edg\/([0-9\.]+)/',
        'Opera' => '/OPR\/([0-9\.]+)/',
        'Internet Explorer' => '/MSIE\s([0-9\.]+);/',
        'Windows' => '/Windows NT ([0-9\.]+)/',
        'macOS' => '/Mac OS X ([0-9_\.]+)/',
        'iOS' => '/iPhone OS ([0-9_\.]+)/',
        'Android' => '/Android ([0-9\.]+)/',
    ];

    // Merge custom rules
    if (!empty($custom_rules) && is_array($custom_rules)) {
        $clients = array_merge($clients, $custom_rules);
    }

    $result = [];

    foreach ($clients as $name => $pattern) {
        if (preg_match($pattern, $user_agent, $matches)) {
            $result[$name] = str_replace('_', '.', $matches[1]);
        }
    }

    return $result;
}

3. Use examples

 $client_info = get_client_version();

// Output client information
echo '<pre>';
print_r($client_info);
echo '</pre>';

4. Support custom extensions

You can detect exclusive client characteristics by passing custom rules, such as customized application clients within the enterprise or some special browser shells.

 $custom_rules = [
    'MyAppClient' => '/MyApp\/([0-9\.]+)/'
];

$client_info = get_client_version(null, $custom_rules);

// Output client information,Include custom content
echo '<pre>';
print_r($client_info);
echo '</pre>';

V. Typical application scenarios

  1. User analysis : Statistics the browser and version distribution used by users.

  2. Functional compatibility processing : Compatibility optimization is made for different browsers or systems.

  3. Security Policy : Identify unsupported clients and prohibit access.

  4. API Restrictions : Restrict certain clients to call certain interfaces based on client information.

VI. Deployment Suggestions

  • It is recommended to call get_client_version at the public entrance of the project and cache the results to $_SESSION or logging.

  • For high concurrency environments, it is recommended to combine Redis or file cache to avoid repeated parsing.

7. Reference link