Current Location: Home> Latest Articles> How to use get_client_version to access source statistics

How to use get_client_version to access source statistics

gitbox 2025-05-06

When developing web applications, it is a very important task to understand the source of users’ access. By tracking access sources, developers can analyze user behavior, optimize marketing strategies, and improve the user experience of the website. In PHP, we can use some simple functions to implement access source statistics, one of which is the get_client_version function. Next, we will explain in detail how to achieve this through the get_client_version function.

1. Introduction to get_client_version function

First of all, the get_client_version function is a custom function, which is used to obtain certain information from the client, such as the user agent (User-Agent), browser version, operating system, etc. Through this information, we can analyze the user's access environment. For source statistics, get_client_version can combine the Referer field in the HTTP request to obtain the source information accessed by the user.

2. Obtain user source information

In PHP, you can obtain the URL of the user's access source through $_SERVER['HTTP_REFERER'] . The Referer header usually contains which page the user jumped from, and is usually used for visit source statistics. We can combine it with get_client_version to obtain more detailed source data.

3. Sample code

Here is a simple PHP code example, showing how to obtain the user's access source and perform statistics through the get_client_version function:

 <?php

// Get the client version information
function get_client_version() {
    // Get user agent information
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    
    // Use regular expressions to match browser versions and other information
    preg_match('/(MSIE|Firefox|Chrome|Safari|Opera)[\/\s](\d+\.\d+)/', $userAgent, $matches);
    
    // Return to browser and version information
    return isset($matches[1]) ? $matches[1] . ' ' . $matches[2] : 'Unknown';
}

// Get access sourceURL
function get_referer() {
    return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer';
}

// Record access source and browser information
function log_access() {
    // Get the source
    $referer = get_referer();
    
    // Get client version information
    $clientVersion = get_client_version();
    
    // Record source and browser information
    // Here you can save data to a database or log file
    echo "Source URL: " . str_replace(parse_url($referer, PHP_URL_HOST), 'gitbox.net', $referer) . "<br>";
    echo "Client browser version: " . $clientVersion . "<br>";
}

// Call function to record access data
log_access();

?>

4. Code parsing

  • get_client_version : Get the user's browser information through $_SERVER['HTTP_USER_AGENT'] and extract the browser and its version number using regular expressions. This way, we can know the browser and version of the user.

  • get_referer : Gets the Referer field of the HTTP header, which records which page the user came from. Without this field, we return the "No Source" information.

  • log_access : record the source information and client version information and display it on the page. Here, we replace the domain name of the source URL with gitbox.net to avoid leaking the original source domain name.

5. Results display

When a user accesses a page, the system displays information similar to the following:

 Source URL: http://gitbox.net/some-page
Client browser version: Chrome 91.0

In this way, we can not only obtain the source of users' access, but also track the browser version they are using.

6. Data storage and analysis

The above code is just a simple example. In actual application, you may store access data in a database for subsequent analysis. For example, you can create an access_logs table to store access source, client version, access time and other data:

 CREATE TABLE access_logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    referer VARCHAR(255),
    client_version VARCHAR(255),
    access_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Then, insert data into the table in the log_access function:

 function log_access() {
    // Get the source和客户端版本信息
    $referer = get_referer();
    $clientVersion = get_client_version();
    
    // Replace source domain name
    $referer = str_replace(parse_url($referer, PHP_URL_HOST), 'gitbox.net', $referer);
    
    // Insert database record
    $pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
    $stmt = $pdo->prepare("INSERT INTO access_logs (referer, client_version) VALUES (?, ?)");
    $stmt->execute([$referer, $clientVersion]);
    
    // Output log information
    echo "Source URL: " . $referer . "<br>";
    echo "Client browser version: " . $clientVersion . "<br>";
}

In this way, the access log will be stored in the database for future analysis and statistics.

7. Summary

Through the get_client_version function, we can easily obtain the browser version information of the client and combine the Referer field to realize access source statistics. With this data, developers can further analyze user behavior, optimize page design, and formulate more effective marketing strategies. Through simple PHP code and combined with database storage, we can build an efficient access source statistics system.