[英]Trouble using Session variable in header redirect
如果用戶未登錄,我將使用以下內容鎖定位置,以便登錄成功后可以返回該位置
$_SESSION['returnURL'] = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES);
我在標頭重定向中使用此會話變量沒有運氣。 當前此變量的內容為: /event.php?title=Test&id=16
if($sql->num_rows==1) {
if (isset($_SESSION['returnURL'])){
$_SESSION['username'] = $username;
header("location:$_SESSION['returnURL'])";
unset($_SESSION['returnURL']);
} else {
$_SESSION['username'] = $username;
header('location:home.php');
}
} else {
die(header("location:login.php?login-failed=true&reason=not_found"));
}
當我用存儲在$_SESSION['returnURL']
的地址替換$_SESSION['returnURL']
的內容時,它可以正常工作。 當我將$_SESSION['returnURL']
變量與標頭一起使用時,出現了問題。
這行的問題:
header("location:$_SESSION['returnURL'])";
固定右括號:
header("location:$_SESSION['returnURL']");
並使用大括號對帶引號的字符串中的數組變量:
header("location:{$_SESSION['returnURL']}");
也:
htmlspecialchars('/event.php?title=Test&id=16', ENT_QUOTES);
// Results in: /event.php?title=Test&id=16
這是一個不同的URL。 您無需在此處轉換為html實體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.