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

Jotform跳转至其他页面时无法获取Cookie值的问题求助

问题描述

我设置了推荐链接逻辑:用户打开带ref参数的推荐链接时,浏览器会写入名为referral的Cookie,即使URL的GET参数被移除,也能通过Cookie获取推荐信息。通过开发者工具确认Cookie在网站所有页面都可用,但当Jotform提交后重定向到我的PHP入库脚本时,Cookie里的推荐值无法被读取存储。我试过给脚本加延迟,但没用;直接打开该PHP脚本时,能正常读取Cookie值。


现有代码

1. 页面Header中设置Cookie的代码(放在<html>标签前)

<?php
if (isset($_GET['ref'])) {
  $cookie_name = "referral";
  $cookie_value = $_GET['ref'];

  setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", ".candidateside.com", true);
}
?>

2. 数据入库的PHP脚本代码

<?php
include_once "error_handler.php";
require_once "connection.php";

sleep(3);

if ($mysqli->connect_error) {
    error_log("Connection failed: " . $mysqli->connect_error);

    header('Location: https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');
    exit;
}

if (isset($_POST['submission_id'])) {
    echo '<pre>';
    print_r($_POST, 1);
    echo '</pre>';

    $sid = $mysqli->real_escape_string($_POST['submission_id']);
    $formID = $mysqli->real_escape_string($_POST['formID']);
    $name = $mysqli->real_escape_string(implode(" ", $_POST['name']));
    $haveYou = $mysqli->real_escape_string($_POST['haveyou']);
    $email = $mysqli->real_escape_string($_POST['email']);
    $phoneNumber = $_POST['phonenumber']['full'];
    $veteranStatus = $mysqli->real_escape_string($_POST['veteranstatus']);
    $ethnicity = $mysqli->real_escape_string($_POST['ethnicity']);
    $dateOfBirth = $_POST['dateof']['year'] . "-" . $_POST['dateof']['month'] . "-" . $_POST['dateof']['day'];
    $howLong = $mysqli->real_escape_string($_POST['howlong']);
    $areYou131 = $mysqli->real_escape_string($_POST['areyou131']);
    $areYou132 = $mysqli->real_escape_string($_POST['areyou132']);
    $willYou = $mysqli->real_escape_string($_POST['willyou']);
    $areYou = $mysqli->real_escape_string($_POST['areyou']);
    $haveYou136 = $mysqli->real_escape_string($_POST['haveyou136']);
    $typeA = $mysqli->real_escape_string($_POST['typea']);

    // Retrieve the referral from the cookie
    $referral = isset($_COOKIE['referral']) ? $_COOKIE['referral'] : null;

    if (is_null($referral)) {
        error_log("Referral cookie not set. Details: 
        IP: " . $_SERVER['REMOTE_ADDR'] . ", 
        URL: " . $_SERVER['REQUEST_URI'] . ", 
        User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . " at " . date('Y-m-d H:i:s'), 0);
    }

    $db_table = "combined_jotform";

    $result = $mysqli->query("SELECT * FROM $db_table WHERE submission_id = '$sid'");

    if ($result->num_rows > 0) {
        /* UPDATE query */
        $result = $mysqli->query("UPDATE $db_table SET name = '$name', haveYou = '$haveYou', email = '$email', phoneNumber = '$phoneNumber', veteranStatus = '$veteranStatus', ethnicity = '$ethnicity', dateOfBirth = '$dateOfBirth', howLong = '$howLong', areYou131 = '$areYou131', areYou132 = '$areYou132', willYou = '$willYou', areYou = '$areYou', haveYou136 = '$haveYou136', typeA = '$typeA', referral = '$referral' WHERE submission_id = '$sid'");
    } else {
        /* INSERT query */
        $result = $mysqli->query("INSERT IGNORE INTO $db_table (submission_id, formID, name, haveYou, email, phoneNumber, veteranStatus, ethnicity, dateOfBirth, howLong, areYou131, areYou132, willYou, areYou, haveYou136, typeA, referral) VALUES ('$sid', '$formID', '$name', '$haveYou', '$email', '$phoneNumber', '$veteranStatus', '$ethnicity', '$dateOfBirth', '$howLong', '$areYou131', '$areYou132', '$willYou', '$areYou', '$haveYou136',  '$typeA', '$referral')");
    }

    $mysqli->close();

    // Set the cookie expiration to the past to delete it
    setcookie("referral", "", time() - 3600, "/", ".candidateside.com", true);

    header('Location: https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');
    exit;
} else {
    exit;
}
?>

解决方案

1. 修复Cookie的SameSite属性

你设置Cookie时未指定SameSite,现代浏览器默认会设为Lax,而Jotform的重定向属于跨站请求,导致Cookie无法被携带。修改Cookie设置代码,添加SameSite=None(必须配合secure=true,仅HTTPS环境可用):

// PHP 7.3+ 写法
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", ".candidateside.com", true, true);

// 兼容旧版PHP的写法
setcookie($cookie_name, $cookie_value, [
    'expires' => time() + (86400 * 30),
    'path' => '/',
    'domain' => '.candidateside.com',
    'secure' => true,
    'samesite' => 'None'
]);

2. 绕过Cookie,直接在表单中传递推荐值

如果跨站Cookie限制无法解决,可在页面中添加隐藏字段,把推荐值写入表单,提交时随POST数据一起传递:

<!-- 在Jotform嵌入的页面中添加隐藏字段 -->
<input type="hidden" name="referral_from_form" id="referral_field">
<script>
// 从Cookie或URL参数中获取推荐值,写入隐藏字段
const referralCookie = document.cookie.split('; ').find(row => row.startsWith('referral='));
document.getElementById('referral_field').value = referralCookie ? referralCookie.split('=')[1] : '';
</script>

然后在PHP脚本中优先读取POST的隐藏字段值:

$referral = isset($_POST['referral_from_form']) ? $_POST['referral_from_form'] : (isset($_COOKIE['referral']) ? $_COOKIE['referral'] : null);

3. 移除无效的sleep延迟

代码中的sleep(3)对Cookie读取无帮助,反而可能导致请求异常,直接删除即可。

4. 调试请求的Cookie携带状态

用浏览器开发者工具的Network面板,查看Jotform重定向到PHP脚本的请求,检查Request Headers中是否包含Cookie: referral=xxx:

  • 若没有,优先排查SameSite设置和跨站域名问题;
  • 若有但PHP读不到,检查php.ini中variables_order是否包含C(确保$_COOKIE能被正确初始化)。

内容的提问来源于stack exchange,提问作者Muhammad Aleem

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 17:21:02