views:

42

answers:

1

My http_referer says this:

   http://www.domain.com/search/?etcetcetcetc...

I need to compare my http_referer to look for this:

   http://www.domain.com/search

And if the first part of the referer is this, then do some code...

Ex:

    if($_SERVER['HTTP_REFERER']=='http://www.domain.com/search'){
          do stuff...

But first I think I need to strip everything after the word "search".

I am not good at regular expressions and this kind of stuff, so help is appreciated... Thanks

+5  A: 

If you just want to check if the string begins with your domain, use strpos:

if(strpos($_SERVER['HTTP_REFERER'], 'http://www.domain.com/search') === 0) {
    // do stuff
}
Michael Mrozek
@the rook, strpos returns both 0 (meaning a match starting at the first char) and false (meaning no match at all) meaning `if(strpos())` would act as if there was no match at all if there's actually a match starting at the first char. Michael Mrozek has it 100% correct. Shortening to `if(strpos())` is incorrect in this circumstance. For functions that return only booleans (and not a mix of bool and int), however, that would be the preferred style.
Frank Farmer