0

I'm trying to make a whois check script.

User can submit some domain address and then get a message if it's available or not.

$_POST['url'] is the submitted value by user.

How do I know if this variable is a domain name address?

It should give true for domains like:

http://google.com
www.google.com
http://www.google.com
google.com

Same for javascript (I'm using ajax-validation also)?

Jasper
  • 5,090
  • 11
  • 34
  • 41

2 Answers2

6

If you want to check if the url is a valid url you could use filter_var() with the FILTER_VALIDATE_URL filter.

filter_var($_POST['url'], FILTER_VALIDATE_URL)
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
slinzerthegod
  • 615
  • 6
  • 17
  • @Steve Easy fix, simply prepend http:// if there is none, then use this function. – Tom van der Woerdt Dec 21 '11 at 15:23
  • @Madmartigan ok, do you know some solution for javascript check? – Jasper Dec 21 '11 at 15:25
  • 1
    @Steve I'm sorry I missed your example urls. I thought that you wanted to check if the url was valid. An url without protocol is not an valid url. – slinzerthegod Dec 21 '11 at 15:29
  • @Steve: IMO, either enforce a strict URL format, either by validation or formatting the string yourself, then just pass it over to the Whois API. The thing is, you are trying to accept and validate strings that simply are not valid URLs by themselves, like `this.one`. – Wesley Murch Dec 21 '11 at 15:31
1

You can use the following code:

Example:

$url = "http://0gate.com"; // you can use instead - $_POST['url']
if (!preg_match("/^[http|https]*[:\/\/]*[A-Za-z0-9\-_]+\.([A-Za-z]{3,4})+([\.A-Za-z]{3})*$/i", $url)) {
  echo "The domain [not valid - false]";
}else{
  echo "The domain is [valid - true]";
}
Lion King
  • 32,851
  • 25
  • 81
  • 143
  • 3
    Oh you mean like [this](http://stackoverflow.com/a/276525/398242)? If you are going to copy/paste answers, at least attribute your source. – Wesley Murch Dec 21 '11 at 15:11
  • If you only need the host you can use `$host = parse_url($url, PHP_URL_HOST);` – Grexis Dec 21 '11 at 15:13
  • 2
    @LionKing: No offense, it was just blatantly obvious that you copied the linked answer and changed a few characters. – Wesley Murch Dec 21 '11 at 15:16
  • @Lion King Helping correctly when a question is already answered would be linking to the question in a comment and/or flagging this question. – Repox Dec 21 '11 at 15:20
  • what, if its not a domain, how do we know? false on $getdomain? – Jasper Dec 21 '11 at 15:20
  • @Steve: The value will be NULL. Quoting the [docs](http://us.php.net/parse_url): `If the requested component doesn't exist within the given URL, NULL will be returned.` – Wesley Murch Dec 21 '11 at 15:22
  • Do not use this regex, it is wrong on so many levels. You do not know about ccTLDs for example? – Patrick Mevzek Jan 04 '18 at 15:13