n92
n92

Reputation: 7602

How to check the string contains the match in PHP

I have to check that the URL does not contains a string.

$url = $_SERVER['HTTP_REFERER'];
$match1 = "http://myapp.com";
$match2 = "http://www.myapp.com";

I want to check that the value in $url does not contain either $match1 and $match2. How do I do this?

Upvotes: 0

Views: 277

Answers (3)

stary
stary

Reputation: 36

In my opinion,matching the string or matching a pattern which contains multiple url forms is what you need think. such as :

  1. http://myapp.com
  2. http://myapp.com/
  3. http://www.myapp.com
  4. http://www.myapp.com/
  5. ...

considering all of the situations!

Note:HTTP_REFERER can be forged.

so,you may validate it like this:

if ($url = filter_var($url, FILTER_VALIDATE_URL)){
   if(!in_array( parse_url($url,PHP_URL_HOST), array('myapp.com','www.myapp.com',...) )){
       // do something
   }
}

Upvotes: 1

Pete Frost
Pete Frost

Reputation: 86

Be aware that the referrer string is not always set (browsers may decided not to send it) so don't rely on it for important functionality.

Upvotes: 1

Make use of strpos() in PHP

<?php
$yoururlarray=['somewebsite.com','somewebsite2.com'];
$urls=implode(',',$yoururlarray);
if(strpos($_SERVER['HTTP_REFERER'],$urls)!==false)
{
    // Found a match so write the ignore code here as you want !
}

Upvotes: 1

Related Questions