簡體   English   中英

如果 cookie 設置基於 url,則設置 cookie 並重定向

[英]Set cookie and redirect if cookie set based on url

我正在運行 woocommerce/wordpress 並注意到客戶刷新感謝頁面會導致谷歌分析記錄多筆交易。

他們訪問的url是:/checkout/order-received/24420/?key=wc_order_a5m1jzQDJLjjr

我想我需要使用該訂單號設置一個 cookie。 如果 cookie 存在,則在分析代碼運行之前重定向。 從而防止重新加載感謝頁面..(我希望)

我不知道是誰從 url 中提取數字 24420 並設置 cookie。 在 Cookie 上重定向我可以弄清楚。

謝謝

/J

檢索訂單 ID

$order_id = wc_get_order_id_by_order_key( $_GET['key'] );

我建議使用 session 而不是 cookie,這樣您就可以執行類似的操作

if(isset($_SESSION['order_'.$order_id])) {
    header("Location: /where-you-want");
    die;
} else {
    $_SESSION['order_'.$order_id] = 1;
}

Please note that both Session and Cookies need to be set before any other output on the page , so be sure that the code you're running is executed before any HTML is printed.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM