Harea Costicla
Harea Costicla

Reputation: 817

Get only a specified url

I want to check only sites finished in : *.test.com and *.test1.com I tried :

if((preg_match('.\w+.test.com', $_SERVER['HTTP_ORIGIN'])) or (preg_match('.\w+.test1.com', $_SERVER['HTTP_ORIGIN']))){
}

Upvotes: 2

Views: 96

Answers (2)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76395

For starters, you want the most reliable way to break down any given url. For this, you can use PHP's built-in parse_url function:

$host = parse_url($url, PHP_URL_HOST);

Then, you want to check whether or not it ends in test.com or test1.com, which you can do using a regex:

if (preg_match('/test1?\.com$/', $host)) {
    //match
}

The regex works like this:

  • test: matches a string literal "test"
  • 1?: Matches a literal 1, but it's optional, so both test and test1 will match
  • \.: a literal dot match
  • com: literal match for com
  • $: end of string. The expression will only match if the string ends in test.com or test1.com.

Just a word of warning: $_SERVER['HTTP_ORIGIN'], and in fact almost none of the $_SERVER values are to be trusted. You can read more on the subject here ( + linked pages)

How secure is HTTP_ORIGIN?

Upvotes: 6

Rafael Shkembi
Rafael Shkembi

Reputation: 776

You could use str_pos. Str_pos will search inside the string,link,url.. The documentation link

Here is a little example

if(strpos($_SERVER['HTTP_ORIGIN'], 'test.com') !== false) {
    echo "found it";
}

Upvotes: 0

Related Questions