1

I have a client. Let's say their domain is www.mydomain.com.

We are creating a new page at www.mydomain.com/newsection.

On the /newsection page, the client would like to add a link that says "Go back to main site" (www.mydomain.com) ONLY if the user was previously on the main site before.

So, I set up a code snippet of PHP like this:

<?php
$referral = $_SERVER['HTTP_REFERER'];
if (preg_match('/http://www.mydomain.com/', $referral)) {
echo "from My Domain";
} else {
echo "not from My Domain";
}
?>

However, this always returns false ("Not from My Domain"), even if the user is coming from mydomain.com.

Are there any obvious syntax errors or other logic issues that I'm not getting?

Is the issue that my new page (www.mydomain.com/newsection) is still on that main domain?

Ash
  • 345
  • 1
  • 5
  • 16
  • 1
    Enable `error_reporting`, then error becomes obvious. – mario Apr 06 '12 at 14:21
  • possible duplicate of [Convert eregi to preg_match - what is Unknown modifier 'F'?](http://stackoverflow.com/questions/3451787/convert-eregi-to-preg-match-what-is-unknown-modifier-f) – mario Apr 06 '12 at 14:23

3 Answers3

2

The slashes in the expression need to be escaped because they're also the delimiter. Alternatively, you can choose another delimiter:

preg_match('~http://www\.mydomain\.com~', $referral)

Note I also escaped the .s, which are special characters in regular expressions. They won't break the regex, but they match any character - probably not what you want.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • THANK YOU so much. I knew it was probably something like that, but I just couldn't figure it out. Really, really appreciate it. – Ash Apr 06 '12 at 14:24
1

You can just use strpos

if (strpos($referral, 'http://www.mydomain.com') !== false) {
Jarosław Gomułka
  • 4,935
  • 18
  • 24
0

Turn on error_reporting when developing. You're actually getting something like the following warning, which should help you (especially combined with minitech's answer):

Warning: preg_match() [function.preg-match]: Unknown modifier '/' in

MueR
  • 977
  • 7
  • 13