1

I have a problem using preg_match. (I'm very new to regular expressions)

if(preg_match('^/http:\/\/myWebsite\.com\/', $_SERVER['HTTP_REFERER'])) {
return true;
}
else{
return false;
}

Always returns false and it shouldn't. Where am I wrong?

UPDATE

Hi everybody ! Thanks all of you for your help ! Every check is fine now

matteo
  • 147
  • 1
  • 1
  • 9
  • 2
    You need to configure PHP to display errors. That way you'll see: `Warning: preg_match(): No ending delimiter` – Álvaro González Jan 16 '12 at 16:59
  • 1) Why not look at available patterns, 2) You don't use case insensitive matching. 3) Delimiter 4) Is the referer always http, https? might be more suited – Layke Jan 16 '12 at 16:59
  • * [converting ereg to preg (missing regex delimiters)](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario Jan 16 '12 at 16:59

4 Answers4

2

Your regular expression is missing the delimiters, as PHP should have warned you:

$_SERVER['HTTP_REFERER'] = 'http://myWebsite.com/foo.html';
var_dump( preg_match('^/http:\/\/myWebsite\.com\/', $_SERVER['HTTP_REFERER']) );

... triggers:

Warning: preg_match(): No ending delimiter

Since you are allowed to choose your own delimiter, it's simpler to pick one that's not in the text:

preg_match('@^http://myWebsite\.com/@', $_SERVER['HTTP_REFERER'])

Additionally, if the text is not fixed (not this case I presume), PHP can escape it for you:

preg_match('/^' . preg_quote('http://myWebsite.com/', '/') . '/', $_SERVER['HTTP_REFERER'])

I suggest you configure your development box to display all possible errors. You have several ways to do so:

  1. Edit your php.inifile:

    error_reporting = E_ALL | E_STRICT
    display_errors = On
    
  2. Put this on top of your script:

    <?php
    
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', TRUE);
    
  3. If PHP runs as Apache module, you can also use an .htaccess file:

    # Print E_ALL | E_STRICT from a PHP script to get the appropriate number:
    php_value error_reporting 2147483647
    php_flag display_errors on
    
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thank you very much for taking time to help me ! Everything runs smooth now ! Thanks again. – matteo Jan 19 '12 at 17:48
1
return (boolean) preg_match('#^http://myWebsite\.com/#i', $_SERVER['HTTP_REFERER']);

You can choose your own delimiter to make things easier. Here I've chosen #.

I also added an i modifier at the end to make the search case-insensitive.

I removed the if/else branches and just returned the result of preg_match (type-casted to boolean).

webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • +1 for using #. I find I rarely use them in regex and hence no need to escape, and they stand out from the thorny spike look of the rest of the regex. – Aram Kocharyan Jan 16 '12 at 17:04
  • Thank you very much for taking time to help me ! The i parameter is really handy ! Thanks a lot. – matteo Jan 19 '12 at 17:48
0

It should be '/^http:\/\/myWebsite\.com\//i' (note the caret (^) position, and the i for case-insensitive matching)

Philippe Plantier
  • 7,964
  • 3
  • 27
  • 40
0

preg_match requires a valid delimiter. It may be throwing a warning that you aren't seeing. You should place the carat ^ after the opening /.

Also try error_reporting(E_ALL); to see any warnings etc.

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96