getOrCreateCorrelationId(); // Make globally available $_SERVER['CORRELATION_ID'] = $correlationId; // Set response header for tracing if (!headers_sent()) { header("X-Correlation-ID: {$correlationId}"); } $next(); } /** * Get existing correlation ID from header or generate new one. */ private function getOrCreateCorrelationId(): string { // Check for existing correlation ID in headers if (isset($_SERVER['HTTP_X_CORRELATION_ID']) && $_SERVER['HTTP_X_CORRELATION_ID'] !== '') { return $_SERVER['HTTP_X_CORRELATION_ID']; } // Generate new correlation ID return bin2hex(random_bytes(8)); } /** * Get the current correlation ID. */ public static function getCorrelationId(): string { return $_SERVER['CORRELATION_ID'] ?? bin2hex(random_bytes(8)); } }