Problem using an if statement wit hHTTP_REFERRER?

Im trying to redirect the user depending on the global variable.

$referrer = $_SERVER['HTTP_REFERER'];
if ($referrer = "http://localhost/masterasp/submit_a_review.php") {
	header( "location: 	submit_a_review.php");
	exit();
} else if ($referrer = "http://localhost/masterasp/become_a_provider.php") {
	header( "location: 	become_a_provider.php");
	exit();
}

It seems to always go to the submit_a_review.php no matter what, is my logic right?

The conditions in your IF statements are assignments, not equality checks. Because a non-empty string in PHP is a truthy value, the return value from the assignment statement will be true (executing the first IF statement body always).

To get around this common mistake of using assignments instead of equality checks, some people use yoda conditions, where the string literal is used as the left-hand operand, and the variable to check against is used as the right-hand operand. This will error out when a single equals is used (for an assignment), since you cannot assign a value to a literal:

if ("http://localhost/masterasp/submit_a_review.php" === $referrer) {
	header( "location: 	submit_a_review.php");
	exit();
} else if ("http://localhost/masterasp/become_a_provider.php" === $referrer) {
	header( "location: 	become_a_provider.php");
	exit();
}
1 Like

Rule #1: Never rely on http_referrer.
Rule #2: Never rely on http_referrer.

= != ==. (That statement actually makes sense, sadly.)
As tpunt says, you’ve confused assignment for comparison. Very common occurrence.

I would not use referrer for this; perhaps re-look at your flow logic?

Yes, as @StarLion said, the http_referer header is generally eschewed because it is unreliable (since it can be turned off in a user’s browser, effectively rendering your code snippet useless). Perhaps as an alternative solution, you could use a session to capture the previous page, and then check to see firstly if that session exists on the next page, and if so then compare its value using your two IF statements above.

I will forget about using the HTTP_REFERRER thing in a bit, but for now am trying to get the redirecting working.
It isn’t working as of this moment and cant figure out if im missing something. but am using

	  if ("http://localhost/masterasp/submit_a_review.php" === $referrer) {
		  header( "location: 	submit_a_review.php");
		  exit();
	  } else if ("http://localhost/masterasp/become_a_provider.php" === $referrer) {
		  header( "location: 	become_a_provider.php");
		  exit();
	  }

Is anything at all being output by the script before it gets to this point? Even a single character of output would prevent headers being able to be written as the headers need to be written before the content.

As felgall said, Define ‘isnt working’. Blank screen may indicate a syntax error; being redirected to the wrong page is a thing. Try echoing “Moo” after the last line of your code; you have an If, an Else If… but nothing if neither of the conditions are met. You may be running into the problem you’re skipping - are you sure that http_referrer is set?

I had a stupid syntax error, geez I hate it when that happens.