[英]Using a variable within a header(“location: ”)
我在頁面上產生了一個變量,然后要在位置重定向的末尾輸出它,我已經通過將其回顯並將其放到瀏覽器中來檢查了sendlink,而且一切似乎都是正確的,不確定為什么這不是正確的工作的
$sendlink = "landing.php?destination1=" . $destination1 . "&destination2=" . $destination2 . "";
if($destination1 & $destination2 != ""){
header( "Location: /" . $sendlink );
}
看到所有其他答案彈出...。
首先,您在條件語句中缺少了“&”號。
if($destination1 & $destination2 != "")
^ missing an ampersand here
其中應該有兩個,並且您還需要對兩個變量使用相同的邏輯。
if($destination1 != "" && $destination2 != "")
或同時在兩個條件上使用條件empty()
。
即: !empty()
。 !
運算符代表“不”為空。
參考文獻:
另一件事; 最好添加一個exit;
在標頭之后,下面是否應該包含更多代碼。 否則,它可能想要繼續執行腳本的其余部分。
參考:
確保沒有在標頭之前輸出。
如果您收到標頭發送通知,請查閱以下內容:
為此,有很多方法。
方法1 !empty()
:
if((!empty($destination1)) && (!empty($destination2))) {
header( "Location: /" . $sendlink );
}
方法2 isset()
(未設置):
if((isset($destination1)) && (isset($destination2))) {
header( "Location: /" . $sendlink );
}
方法3 !==''
:
if(($destination1 !== '') && ($destination2 !== '')) {
header( "Location: /" . $sendlink );
}
提供: 弗雷德-ii-
if($destination1 != "" && $destination2 != "")
或使用empty()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.