prepare(" SELECT id, client_url, status FROM survey_urls WHERE project_id = ? AND unique_identifier = ? LIMIT 1 "); $stmt->execute([$project_id, $unique_id]); $result = $stmt->fetch(PDO::FETCH_ASSOC); if (!$result) { header('HTTP/1.0 404 Not Found'); echo "Survey URL not found or expired"; exit; } // Mark as clicked if still available or sent (sent = dispatched by mailer but not yet clicked) if (in_array($result['status'], ['available', 'sent'])) { $stmt = $pdo->prepare(" UPDATE survey_urls SET status = 'clicked', clicked_at = NOW() WHERE id = ? "); $stmt->execute([$result['id']]); } // Log the click error_log("Survey URL clicked: Project=$project_id, UniqueID=$unique_id, PrevStatus={$result['status']}"); // Redirect to client survey URL header('Location: ' . $result['client_url']); exit; } catch (Exception $e) { error_log("Survey proxy error: " . $e->getMessage() . " | Encoded: " . $encoded); header('HTTP/1.0 500 Internal Server Error'); echo "Error loading survey. Please contact support."; exit; } ?>