0

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 );
}
Lewis Smith
  • 1,271
  • 1
  • 14
  • 39

3 Answers3

2

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 );
}
Panda
  • 6,955
  • 6
  • 40
  • 55
  • I think OP wants the opposite of each of the first two (`!empty()` and `!isset()`). Also, there's no need for the parentheses around those expressions. – elixenide Feb 11 '16 at 16:11
2

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.

I.e.: !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:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Provided by: Fred -ii-

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

Community
  • 1
  • 1
Lewis Smith
  • 1,271
  • 1
  • 14
  • 39