我在网址中传递了一个变量,例如:www.example.com/mypage?gp = 32442 注意:这是一个WordPress网站。
保存gp变量之后,我将它与当前页面ID进行比较,如果它们相等,我设置cookie和会话并重定向到同一页面,而不是“?gp = 32442”最后的字符串。
Cookie正常运行,但我还添加了会话变量,以防有人禁用Cookie。
当我禁用cookie测试时,重定向后会话变量为空。我测试时没有重定向,会话变量被正确添加。
如何在重定向后保留会话变量?
session_start();
if(isset($_GET["gp"])){
$url_post_id = $_GET["gp"];
}
$current_id = get_the_ID();
// $pageURL is the url without the "?gp=32442" at the end
if($current_id == $url_post_id){
setcookie("gpcid", $url_post_id, time() + (60 * 60 * 24 * 14));
$_SESSION['gpsid'] = $url_post_id;
header('Location: '.$pageURL.'');
}
if($_COOKIE["gpcid"] == $current_id || $_SESSION['gpsid'] == $current_id ){
the_content();
} else {.......}