wordpress 如何根据cookie状态更改url

juud5qan  于 2022-11-22  发布在  WordPress
关注(0)|答案(1)|浏览(144)

我正在一个WordPress插件上工作我设置cookie当用户点击以下按钮之一
一个月一个月一个月一个月
这里是

我想使用cookie状态在网站的所有页面一旦它被设置为查询参数。
我已经将动态cookie作为查询参数(根据需要)附加到url,然后尝试如下重新加载它
`

$to_laod = $_SERVER['SCRIPT_URI'].'?filter_gender='.$_COOKIE['gender'].'&query_type_gender=or'; //abc.com/prod-category/fashion/?filter_gender=men&query_type_gender=or

        header("Status: 301 Moved Permanently");
            header("Location:". $to_laod);

如何abc.com/prod-category/fashion/?filter_gender=men&query_type_gender=or解决这个问题?
我已经尝试了标题(“位置:链接”),但我不知道我是否在正确的道路上

svmlkihl

svmlkihl1#

当URL上的filter_gender与cookie中的值匹配性别时,你会在重定向循环中迷失。所以这样的逻辑检查应该可以帮助你避免太多的重定向错误。

if($_COOKIE['gender'] !== $_GET['gender']) {
    $to_laod = $_SERVER['SCRIPT_URI'].'?filter_gender='.$_COOKIE['gender'].'&query_type_gender=or'; //abc.com/prod-category/fashion/?filter_gender=men&query_type_gender=or

    header("Status: 301 Moved Permanently");
    header("Location:". $to_laod);
}

相关问题