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


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() .!运算符代表"不"为空。

引用:

  • http://php.net/manual/en/language.operators.logical.php
  • http://php.net/manual/en/function.empty.php

另一件事;最好在标头后添加一个exit;,如果下面有更多代码。否则,它可能希望继续执行脚本的其余部分。

参考:

  • http://php.net/manual/en/function.header.php

同时确保您没有在标题之前输出。

如果您收到标头已发送通知,请参阅以下内容:

  • 如何修复PHP中的"标头已发送"错误

要实现这一点,有很多方法。

方法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 );
}

提供者:Fred -ii-

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