我的主页有4个链接到不同的子页面(第1页、第2页等),当访问者选择一个页面(即/ page -1/)时,我希望他们在返回网站时被直接路由到该页面。我试图使用cookie来存储选定的页面,并在返回时检查cookie以重新定向到先前选择的URL。
function set_pref_cookie(){
$root = $_SERVER['REQUEST_URI'];
if ($root !='/'){
setcookie('pref_sel',$root, time()+60*60*24*5, "/");
}
if (isset($_COOKIE['pref_sel'])){
header('Location:' . $_COOKIE['pref_sel']);
exit;
}
}
add_action('init','set_pref_cookie');
这可以设置cookie,但是返回到站点时,我会得到一个重定向循环。
发布于 2020-05-23 04:25:08
您有一个重定向循环原因:
<代码>H19您将我重定向到页-1<代码>H 210H 111等等…H 212f 213/code>
在重定向之前,只需检查我是否还没有出现在好的页面上:)
只需改变这个:
if (isset($_COOKIE['pref_sel'])){
header('Location:' . $_COOKIE['pref_sel']);
exit;
}
对此:
if (isset($_COOKIE['pref_sel']) && $_COOKIE['pref_sel'] != $root){
header('Location:' . $_COOKIE['pref_sel']);
exit;
}
并且它不会在无限循环中重定向。
https://stackoverflow.com/questions/61959078
复制相似问题