HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux vmi3050980 6.8.0-90-generic #91-Ubuntu SMP PREEMPT_DYNAMIC Tue Nov 18 14:14:30 UTC 2025 x86_64
User: gurumedia_st_usr (1012)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/gurumedia_st_usr/data/www/gurumedia.store/frd-src/abc/meta_cookies.php
<?php
/**
 * ===============================================================
 * Facebook _fbp & _fbc Cookie Generator (Visit-Trigger Refresh)
 * ---------------------------------------------------------------
 * Version: 1.5 (Final)
 * Author: SpiderEcommerce.com
 * ===============================================================
 * _fbp: fb.1.<timestamp_ms>.<random_number>
 * _fbc: fb.1.<timestamp_ms>.<fbclid>
 * ---------------------------------------------------------------
 * _fbp -> Refresh when visitor enters site (same ID, new expiry)
 * _fbc -> Regenerate only when new fbclid found in URL
 * ===============================================================
 */

$FB_DEBUG_LOG_ENABLED = false; // 🟢 true = enable log | 🔴 false = disable log
$FB_LOG_PATH = __DIR__ . '/x_fb_cookie_log.txt';

/**
 * Debug log writer (optional)
 */
function fb_log_event(string $message): void
{
    global $FB_DEBUG_LOG_ENABLED, $FB_LOG_PATH;
    if ($FB_DEBUG_LOG_ENABLED !== true) return;

    if (!is_dir(dirname($FB_LOG_PATH))) {
        mkdir(dirname($FB_LOG_PATH), 0775, true);
    }

    $time = date('Y-m-d H:i:s');
    file_put_contents($FB_LOG_PATH, "[$time] $message" . PHP_EOL, FILE_APPEND);
}

/**
 * Handle _fbp cookie — Visit-trigger refresh
 */
if (!function_exists('generate_fbp_cookie')) {
    function generate_fbp_cookie(): string
    {
        $lifetime_days = 90;
        $existing_fbp = $_COOKIE['_fbp'] ?? null;

        if (!empty($existing_fbp)) {
            // ✅ Renew expiry, keep same ID (Meta SDK behavior)
            setcookie('_fbp', $existing_fbp, [
                'expires'  => time() + ($lifetime_days * 24 * 60 * 60),
                'path'     => '/',
                'domain'   => $_SERVER['HTTP_HOST'],
                'secure'   => isset($_SERVER['HTTPS']),
                'httponly' => false,
                'samesite' => 'Lax'
            ]);
            fb_log_event("🔁 _fbp refreshed (same ID kept): {$existing_fbp}");
            return $existing_fbp;
        }

        // 🆕 Generate new _fbp for new visitor or expired cookie
        $timestamp_ms = round(microtime(true) * 1000);
        $random_number = mt_rand(1000000000, 9999999999);
        $fbp = "fb.1.{$timestamp_ms}.{$random_number}";

        setcookie('_fbp', $fbp, [
            'expires'  => time() + ($lifetime_days * 24 * 60 * 60),
            'path'     => '/',
            'domain'   => $_SERVER['HTTP_HOST'],
            'secure'   => isset($_SERVER['HTTPS']),
            'httponly' => false,
            'samesite' => 'Lax'
        ]);

        fb_log_event("🆕 New _fbp created: {$fbp}");
        return $fbp;
    }
}

/**
 * Handle _fbc cookie — Regenerate only when new fbclid present
 */
if (!function_exists('generate_fbc_cookie')) {
    function generate_fbc_cookie(): ?string
    {
        $lifetime_days = 90;
        $fbclid = $_GET['fbclid'] ?? null;
        $current_fbc = $_COOKIE['_fbc'] ?? null;

        // 🆕 Only regenerate if new fbclid found or cookie missing
        if ($fbclid && (empty($current_fbc) || !str_contains($current_fbc, $fbclid))) {
            $timestamp_ms = round(microtime(true) * 1000);
            $fbc = "fb.1.{$timestamp_ms}.{$fbclid}";

            setcookie('_fbc', $fbc, [
                'expires'  => time() + ($lifetime_days * 24 * 60 * 60),
                'path'     => '/',
                'domain'   => $_SERVER['HTTP_HOST'],
                'secure'   => isset($_SERVER['HTTPS']),
                'httponly' => false,
                'samesite' => 'Lax'
            ]);

            fb_log_event("🆕 _fbc generated (fbclid={$fbclid}): {$fbc}");
            return $fbc;
        }

        fb_log_event("✅ _fbc valid, no new fbclid. {$current_fbc}");
        return $current_fbc;
    }
}