Current Location: Home> Latest Articles> Use get_client_version to identify mobile device access

Use get_client_version to identify mobile device access

gitbox 2025-05-06

In modern web development, it is a common requirement to identify whether users access websites through mobile phones, tablets, or computers. By obtaining the User-Agent information of the client, we can identify the user's device type. There are some methods in PHP that can help us achieve this goal, and the get_client_version function is a very practical tool.

This article will introduce how to identify whether a user uses a mobile phone or a computer to access the website through the get_client_version function, and provide a simple PHP code example.

1. Obtain user agent information

The get_client_version function usually depends on the User-Agent field sent by the browser. The User-Agent field contains information such as client device, operating system, browser, etc. By analyzing this information, we can determine the type of device the user is using.

In PHP, we can get the user's User-Agent string via $_SERVER['HTTP_USER_AGENT'] . For example:

 $user_agent = $_SERVER['HTTP_USER_AGENT'];

This code stores the user's User-Agent information in the $user_agent variable.

2. Parsing User-Agent string

The core of the get_client_version function is to parse the User-Agent string to determine the device type. Here is a simple PHP example that demonstrates how to determine whether a user's device is a mobile phone or computer through regular expressions.

 function get_client_version() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];

    // Determine whether it is a mobile phone
    if (preg_match('/(iPhone|iPad|Android|Windows Phone)/i', $user_agent)) {
        return 'mobile'; // return mobile Expressed as a mobile phone
    }

    // 默认return电脑
    return 'desktop'; // return desktop Expressed as a computer
}

// Calling a function to determine device type
$device_type = get_client_version();
if ($device_type === 'mobile') {
    echo "You are accessing the website via your mobile phone。";
} else {
    echo "You are accessing the website via your computer。";
}

In this code example, the get_client_version function checks whether the User-Agent string contains some specific keywords, such as iPhone , Android , or Windows Phone , etc. If these keywords are included, it can be determined that the user is using the mobile phone to access the website. The function returns mobile , otherwise it returns desktop .

3. Custom extensions

In addition to basic mobile phone and computer judgment, you can also expand this function according to specific needs. For example, if you need to support the judgment of tablet devices, you can add recognition of iPad and Tablet .

 function get_client_version() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];

    // Determine whether it is a mobile phone
    if (preg_match('/(iPhone|iPad|Android|Windows Phone)/i', $user_agent)) {
        return 'mobile'; // return mobile Expressed as a mobile phone
    }

    // Determine whether it is a tablet
    if (preg_match('/(iPad|Tablet)/i', $user_agent)) {
        return 'tablet'; // return tablet Expressed as a tablet
    }

    // 默认return电脑
    return 'desktop'; // return desktop Expressed as a computer
}

This allows you to further customize the processing based on the device type, such as adjusting the page layout to suit different devices.

4. Summary

Use the get_client_version function to easily determine whether the user is accessing your website through a mobile phone, tablet or computer. By parsing User-Agent strings and making judgments based on their content, you can optimize the user experience of the website and ensure that the page has a good display effect on different devices.