$_SERVER['HTTP_REFERER'] does not work alltime

1.8k views Asked by At

I am filtering a page with $_SERVER['HTTP_REFERER'].

Let "pageb.php" filter whether a user have come from "pagea.php" (although it is not reliable) by $_SERVER['HTTP_REFERER'].

It works well until "pagea.php" has a preceding query string like "pagea.php?q=10". Then if a user goes from "pagea.php?q=10" to "pageb.php" it is not detected by "pageb.php":

if($_SERVER['HTTP_REFERER']='http://pagea.php'){
 echo 'This user has come from page a';
}else{
 echo 'This user has come from another page';
}

How can I detect a user came from "pagea.php" even with preceding query string q=10?

2

There are 2 answers

0
Joel L On BEST ANSWER

Use strpos() to check if a string contains another string:

<?php
if (strpos($_SERVER['HTTP_REFERER'], 'pagea.php') !== false) {
  echo 'from page a';
}
0
James Franklin On

use comparision operator not assignment if($_SERVER['HTTP_REFERER']='http://pagea.php'){

= will be ==