You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PHP获取各浏览器唯一标识?网站用户追踪替代IP方案咨询

Hey there! Let's tackle your user tracking questions—IP addresses are definitely unreliable for this, so I'll walk you through better alternatives and how to grab browser unique IDs with PHP.

替代IP的稳定用户唯一标识方案

IPs shift constantly (thanks to dynamic IPs, VPNs, and shared networks), so you need more robust methods. Here are your best options:

  • Persistent Cookies with a Unique ID
    This is the most common approach. Generate a random, unique string (like a UUID) when a user first visits, store it in a long-lived cookie, and use that ID to track their sessions.
    Example PHP code to set this up:

    if (!isset($_COOKIE['user_unique_id'])) {
        // Generate a UUID v4 (random)
        $userId = sprintf(
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff)
        );
        // Set cookie to expire in 1 year (HttpOnly for security)
        setcookie('user_unique_id', $userId, time() + 31536000, '/', '', false, true);
    } else {
        $userId = $_COOKIE['user_unique_id'];
    }
    // Now use $userId to track the user in your database
    

    Pros: Simple to implement, works for most users.
    Cons: Users can delete cookies, so tracking breaks if they do.

  • User Accounts (The Most Reliable Option)
    If you can get users to sign up or log in, using their user ID from your database is the gold standard. It's completely stable across devices and IP changes, and you get added benefits like personalized experiences.

  • Device Fingerprinting
    Create a unique "fingerprint" by combining multiple browser/device attributes (user agent, screen resolution, installed plugins, timezone, etc.). Hash these attributes to get a string that identifies the user's browser/device.
    You'll need JavaScript to collect most of these details, then send the hash to your PHP backend. For example:

    // Frontend JS to collect fingerprint data
    function generateFingerprint() {
        const data = [
            navigator.userAgent,
            navigator.platform,
            screen.width + 'x' + screen.height,
            navigator.timezoneOffset,
            navigator.language,
            navigator.plugins.length
        ].join('|');
        // Use SHA-256 for better uniqueness (native browser crypto API)
        return crypto.subtle.digest('SHA-256', new TextEncoder().encode(data))
            .then(hash => Array.from(new Uint8Array(hash))
                .map(b => b.toString(16).padStart(2, '0'))
                .join(''));
    }
    // Send this fingerprint to PHP via AJAX on page load
    generateFingerprint().then(fp => {
        fetch('/track.php', {
            method: 'POST',
            body: new FormData().append('browser_fingerprint', fp)
        });
    });
    

    Pros: Doesn't rely on cookies, works even if users delete them.
    Cons: Fingerprints can change if the user updates their browser or settings, and there's a small chance of collisions (two users having identical fingerprints).

用PHP获取浏览器唯一标识

Pure PHP can only get limited browser data (like the user agent), but to create a truly unique browser ID, you need to combine frontend and backend code:

  1. Collect Browser/Device Data with JavaScript
    As mentioned in the fingerprinting section, use JS to gather attributes that PHP can't access directly (like screen size, plugins, timezone, etc.).

  2. Send the Data to PHP
    You can send the generated fingerprint via a POST request, either on page load or through an AJAX call (like the example above).

  3. Process and Store in PHP
    Example PHP code to receive and use the fingerprint:

    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['browser_fingerprint'])) {
        $fingerprint = $_POST['browser_fingerprint'];
        // Store this fingerprint in your database alongside user activity
        // Or compare it to existing records to identify returning users
    }
    

    If you only need a basic (non-unique) identifier, you can use the user agent from PHP, but note that many users will share the same agent:

    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    // Hash it to get a consistent string
    $basicId = hash('sha256', $userAgent);
    

Important Privacy Note

Make sure you comply with regulations like GDPR or CCPA. Always inform users that you're tracking their activity, give them the option to opt out, and avoid storing any sensitive personal data—stick to non-identifiable IDs or fingerprints.

内容的提问来源于stack exchange,提问作者Rainn Mohammed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:36:36