简体   繁体   English

在标头(“ location:”)中使用变量

[英]Using a variable within a header(“location: ”)

I have produced a variable up the page and then want to output it for the end of the location redirect, I have checked the sendlink by echoing it and putting it into the browser and all seems to be right, not sure why this isn't working 我在页面上产生了一个变量,然后要在位置重定向的末尾输出它,我已经通过将其回显并将其放到浏览器中来检查了sendlink,而且一切似乎都是正确的,不确定为什么这不是正确的工作的

$sendlink = "landing.php?destination1=" . $destination1 . "&destination2=" . $destination2 . "";

if($destination1 & $destination2 != ""){
    header( "Location: /" . $sendlink );
}

Seeing all those other answers popping up.... 看到所有其他答案弹出...。

First, you're missing an ampersand in your conditional statement. 首先,您在条件语句中缺少了“&”号。

if($destination1 & $destination2 != "")
                  ^ missing an ampersand here

There should be two of those and you also need to use the same logic for both variables. 其中应该有两个,并且您还需要对两个变量使用相同的逻辑。

if($destination1 != "" && $destination2 != "") or use a conditional empty() on both also. if($destination1 != "" && $destination2 != "")或同时在两个条件上使用条件empty()

Ie: !empty() . 即: !empty() The ! ! operator stands for "not" empty. 运算符代表“不”为空。

References: 参考文献:

Another thing; 另一件事; it's best to add an exit; 最好添加一个exit; after header, should you have more code below that. 在标头之后,下面是否应该包含更多代码。 Otherwise, it may want to continue executing the rest of the script. 否则,它可能想要继续执行脚本的其余部分。

Reference: 参考:

While making sure you're not outputting before header. 确保没有在标头之前输出。

Consult the following if you get a headers sent notice: 如果您收到标头发送通知,请查阅以下内容:

To achieve this, there are quite a few methods. 为此,有很多方法。

Method 1 !empty() : 方法1 !empty()

if((!empty($destination1)) && (!empty($destination2))) {
    header( "Location: /" . $sendlink );
}

Method 2 isset() (Not Set): 方法2 isset() (未设置):

if((isset($destination1)) && (isset($destination2))) {
    header( "Location: /" . $sendlink );
}

Method 3 !=='' : 方法3 !==''

if(($destination1 !== '') && ($destination2 !== '')) {
    header( "Location: /" . $sendlink );
}

Provided by: Fred -ii- 提供: 弗雷德-ii-

if($destination1 != "" && $destination2 != "") or use empty() if($destination1 != "" && $destination2 != "")或使用empty()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM