|
Posted by Toby Inkster on 01/28/07 01:17
Sam wrote:
> so any one have the pattren to check?
Please don't top post.
How sure do you want to be that a domain is real? Do you just want to make
sure that the domain is lexically sane? If so, you want to check that the
domain name has 2 or more components, each of which contains at least two
alphanumeric characters and may contain hyphens, though the hyphen may not
be the first or last character. Each component should terminate with a dot.
Code here:
$domain = 'me.example.net.';
// Note the domain ends with a dot! This is the correct form for
// a fully qualified domain name, even though most people tend to
// leave out the final dot. Including the dot makes the regular
// expression check much easier, so here we want to make sure that
// $domain ends with a dot, and if not add it.
if (!preg_match('/\.$/', $domain)) $domain .= '.';
// Now check if it meets syntax rules.
$domain_ok = preg_match('/^([a-z0-9][a-z0-9\-]*[a-z0-9]\.){2,}$/',
$domain);
This check will allow for a domain like 'foobar.quux.quuux.' even though
it is obviously not a real domain though. To check that it's a real,
registered domain name, which points to a real IP address, we can use
gethostbyname():
// Firstly, do all the above stuff to make sure it it syntactically
// correct. No point wasting network resources calling gethostbyname()
// for a domain that is obviously invalid. Now:
$domain_ok = $domain_ok && !empty(gethostbyname($domain));
// Now $domain_ok is TRUE iff $domain is an Internet host. Yay!
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Navigation:
[Reply to this message]
|