【发布时间】:2013-10-20 13:36:42
【问题描述】:
我在 url 中传递了一个变量,例如:www.example.com/mypage?gp=32442 注意:这是一个 WordPress 网站。
保存 gp 变量后,我将其与当前页面 id 进行比较,如果它们相等,我将设置 cookie 和会话并仅在没有“?gp=32442”的情况下重定向到同一页面最后的字符串。
cookie 工作正常,但我还添加了 session 变量以防有人禁用 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 {.......}
【问题讨论】:
标签: php session redirect session-variables