0

How can we check if URL is only the domain.

http://www.google.pl TRUE
http://www.google.pl/aaaaa FALSE
http://www.google.pl/aaaaa?ahaha=22 FALSE

I want to check if user only write the domain.

2 Answers2

0

Use SERVER_NAME.

$url =  $_SERVER['SERVER_NAME']; //Outputs the base url www.example.com
if($url == $mydomain){
   //do something
 }
nestedl00p
  • 490
  • 3
  • 14
0

parse_url can turn a URL-string ($url) into an array, but can also return parts of that URL. Using that feature we can reconstruct a URL-string that consists only of a scheme and a host. If this matches with the original URL-string ($url), than it must be a domain-only URL

$url = "https://example.com/blabla";
if (parse_url($url, PHP_URL_SCHEME) . '://' . parse_url($url, PHP_URL_HOST) == $url) {
    echo "host only";
} else {
    echo "more than that";
}
Rik Meijer
  • 16
  • 2
  • You should add some explanation. – Mr Mush Dec 15 '15 at 16:20
  • @Rik , there is a problem. I need have / in the end. example: http://www.example.com/ . How can I do that? Thanks! –  Dec 18 '15 at 08:06
  • e mean after .com/ how can i make sure that to accept the host only with / in the end? –  Dec 18 '15 at 10:41
  • You could use trim on your $url. Trim accepts an extra parameter, which is the character to trim. http://php.net/trim – Rik Meijer Dec 18 '15 at 10:58
  • but in that case it fails validation @RikMeijer –  Dec 18 '15 at 11:14
  • Another option is to use stripos. First parameter being $url, second being the parse_url url. Check if it returns 0 (with ===). Sorry for my short answers, I am on a phone – Rik Meijer Dec 18 '15 at 11:30
  • i could fix it. But this doesn't validate if user added .com, .org etc on url. –  Dec 18 '15 at 11:32
  • @RikMeijer now i have like this: parse_url($url, PHP_URL_SCHEME) . '://' . parse_url($url, PHP_URL_HOST).'/' == $url. –  Dec 18 '15 at 11:32