Redirect problem using HTTP_REFERER

Hi,

I’m using this code to redirect visitors who are coming from another site (clicking on my ad on that site) to a page in my site where I can track them:

$referrer = $_SERVER['HTTP_REFERER'];
    if ( strstr($referrer, '://example.com/shop') )
       print ('<meta http-equiv="refresh" content="0; URL=http://www.mysite.com/1.php">');

The problem is that, by this code I can only redirect those who are coming from example.com/shop, but I need to redirect visitors coming from any sub directory or subdomain from example.com, not only a specific url in example.com

Is there any solution?

Thanks

Surely a different (or multiple) string comparison(s) would do the trick:


if (strstr($referrer, '.example.com') || strstr($referrer, '://example.com))

or perhaps just looking for ‘example.com’ without any additional work other than to ensure it’s not ‘myexample.com’ that referred.

Or


if (preg_match('~example\\.com$', parse_url($referrer, PHP_URL_HOST))) {
    // do something
}

This gets the domain from the URL, and checks if that domain ends in “example.com

(if you just use strstr you run the risk of false positives in URLs like [noparse]http://example2.com/?ref=example.com[/noparse], which does contain “example.com”, but that’s not the domain, it’s in the query string)

Good point, I’ve never got my head around regular expressions. So the parse_url will split the ‘example.com’ from the ‘/shop’ in the first example so it’s always at the end of the comparison string?

If you want to know more about them I find http://www.regular-expressions.info/ a very good source!

Yes. Using PHP_URL_HOST it returns the host from any URL, which in the first example will indeed be example.com.
The regular expression says that the string must end in “example.com”, so example.com and all subdomains will match.
You can use parse_url to get more than the host. Check it out in the manual: [fphp]parse_url[/fphp]

Thanks guys.

But I changed it to:

$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match('~example\\.com$', parse_url($referrer, PHP_URL_HOST))) {
    print ('<meta http-equiv="refresh" content="0; URL=http://www.mysite.com/1.php">');
}

And it didn’t work. Actually it didn’t recognize the visitors are coming from example.com, at all, and so the redirect code didn’t work.

Did I do something wrong?

For me, I seem to need a different regular expression:


if (preg_match("/example.com$/", parse_url(PHP_URL_HOST))) {

The dot in .com must be escaped:


if (preg_match("/example\\.com$/", parse_url(PHP_URL_HOST))) {

Otherwise the dot will match any character, instead of just a dot.

And indeed, in my code I forgot a ~ at the end, that’s why it doesn’t work.

Thanks a lot ScallioXTX, your code worked like a charm.

The final code is:

$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match('~example\\.com$~', parse_url($referrer, PHP_URL_HOST))) {
       print ('<meta http-equiv="refresh" content="0; URL=http://www.mysite.com/1.php">');
}

It would be even better if you use HTTP redirect instead of (ab)using Javascript.

$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match('~example\\.com$~', parse_url($referrer, PHP_URL_HOST))) {
       header('Location: http://www.mysite.com/1.php');
}

Make sure you put this before any output (html), otherwise it won’t work.

Absolutely, that would be great.
Thanks man for the help.