0

I need to be able to detect with PHP if a link is from a particular domain. I can not just check if domain is present in the link because it can be faked by appending domain.

Thanks.

santa
  • 12,234
  • 49
  • 155
  • 255
  • Can you provide an example of what you are looking for? It's possible that you just need to use `parse_url()`. – Matthew Dec 13 '10 at 19:27

2 Answers2

3

Just use parse_url() as konforce mentioned. For example:

$url = "http://www.google.com/";
$parts  = parse_url ($url);

print $parts["host"];   // will print www.google.com

// Or, for PHP 5.1 and above
$host =  parse_url ($url, PHP_URL_HOST); // returns www.google.com

Now, the good thing about this is that appending the domain to the end of an url like this:

http://www.google.com/?www.foo.com

Wont work as the host element will still say that the link points to www.google.com and not www.foo.com.

Hope this helps.

mishmash
  • 4,422
  • 3
  • 34
  • 56
0

I believe you'd want check the referrer and make sure you check with double forward slashes, since that's part of the protocol (HTTP/HTTPS) and can't be faked.

Check this link for extra reference: Determining Referer in PHP

I would check against something like...

//www.mydomain.com

//mydomain.com

Community
  • 1
  • 1
jocull
  • 20,008
  • 22
  • 105
  • 149