简体   繁体   中英

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 = "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.

Ie: !empty() . The ! operator stands for "not" empty.

References:

Another thing; it's best to add an 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() :

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

Method 2 isset() (Not Set):

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

Method 3 !=='' :

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

Provided by: Fred -ii-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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