0

how do i send a php variable (boolean) with a redirect. For example a flag is set if a condition is true

if($url = 'someurl.com'){
   $flag = true;
    header('Location: newpage.php?$flag');
    exit();
}

then mypage.php needs to check if its true or false to execute a function

i tried this but it doesn't work

header('Location: mypage.php?$flag');

perhaps there is a better way of doing it?, this is the 2nd redirect on the page, the first redirect checks checks for a login, then it proceeds to check the $flag variable, no output has been done at this stage

thanks in advance for any advice

2
  • 1
    Variables don't get expanded (interpolated) when they're in single-quotes. Use double-quotes instead. Commented Jun 13, 2014 at 9:29
  • Isn't newpage.php?flag=1 something you see every day? That's how. Commented Jun 13, 2014 at 9:29

6 Answers 6

2

Variables don't work in single quotes - Also you need to use a comparison operator in an if statement - not an assignment:

if($url == 'someurl.com')
{
    $flag = 'someText';
    header("Location: newpage.php?$flag");
    exit();
}

or

header('Location: mypage.php?'.$flag);
Sign up to request clarification or add additional context in comments.

1 Comment

@bartek LMAO, I had it right but then went back to improve the answer. Copied the wrong stuff again. Good show!
0

Sending true in a variable wont work for this, you have to set it to $flag="true" and you have to use double quotes.

And a single equals means your setting a variable 2 equals means your checking if a variable is equal to something

if($url == 'someurl.com'){
   $flag = "true";
    header("Location: newpage.php?$flag");
    exit();
}

1 Comment

thank you, the double quotes done the trick i also had some basic syntax errors that i didn't see after spending to long looking at it, sometime the most basic errors are hardest to find because you think you couldn't make such a mistake,
0

Change your if condition to

 if($url == 'someurl.com'){ ....

And the set the header as

 header('Location: mypage.php?flag='.$flag);

Comments

0
$flag = '1';
header('Location: newpage.php?flag='.$flag);

Then in newpage.php, check with $_GET['flag'].

Comments

0

try == operator to check in if() and variable need to correct add with header url use concat(.)

if($url == 'someurl.com'){

and

header('Location: newpage.php?'.$flag);

Comments

0

You need to concatenate php variable with string using dot(.) operator.

Try this.

header('Location: newpage.php?'.$flag);

Also check your condition with comparison operator(==)

  if($url == 'someurl.com') {
     ....
  }

$url = 'someurl.com' it means assigning a string into $url

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.