My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
DomainsClass  
Domain name validation class.
PHPClass, Domains, Featured, Phase-Implementation
Updated Feb 10, 2010 by cdburg...@gmail.com

domains

domain - domain validation and verification

Description

Function

bool validateDomain (string $domain [, bool $verify])

Validates/Verifies a domain name.

note: You will need to make sure you update the reference to the TLD file in the top of this class. See DomainsClass#Summary for more details.

Function

bool validateIP( string $ip [, bool $filter])

Validate and filter an IP address.

Function

string thisHostName()

returns the host name of server if it can be determined, otherwise the public IP address.

Parameters

Validate Domain

domain

The domain name you want to validate/verify
verify (optional)
A boolean setting of either TRUE or FALSE. It determines the the given domain should also be validated by checking for the DNS MX records for the domain.

Validate IP

ip

The IP address you want to validate/filter
filter (optional)
Will return FALSE (INVALID) when set to reserved or private and the IP address falls in one of these ranges.

Return Values

Returns TRUE if the email was validated/verified, FALSE otherwise.

Other variables are also set for reporting purposes: array $class->status ( Returns the status of the validation/verification.)

  • $class->status[0] : bool (true/false)
  • $class->status[1] : string status enumeration (ERROR, INVALID, NOT TESTED, VALID)
  • $class->status[2] : string message
  • $class->status[3] : string server response or specific message for status reason

array $class->servers ( The list of available MX domains )

string $class->valid_domain ( The domain name that was validated )

string $this->this_server (when thisHostName is called directly)

Examples

Example 1: Validating a list of Domain names for RFC compliance

<?php

include('domains.php');

$domains = new Domains;

$my_domains = array(
	'1,run.cops',
	'abc-.uk',
	'1-1.com',
	'-go.com',
	'google.com',
	'mail.yahoo.net',
	'some.other.domain.museum',
	'failed.domain.wa',
	'this-domain-name-will-fail-because-there-are-too-many-characters-in-one-node.com',
	'this-domain-name.will-pass-because-the.domain-name.is-way-too-long.but-is-under.255.characters.and-this-domain-name-is-165-characters.long-which-is-way-too-long.aero'
);

foreach($my_domains as $domain)
{
	echo 'Testing: '.$domain."\n";
	if($domains->validateDomain($domain))
	{
		echo "Domain PASSES!\n";
	}else{
		echo "Domain FAILS!\n";
	}
	echo "============================\n";
}

// expected output
/*
Testing: 1,run.cops
Domain FAILS!
============================
Testing: abc-.uk
Domain FAILS!
============================
Testing: 1-1.com
Domain PASSES!
============================
Testing: -go.com
Domain FAILS!
============================
Testing: google.com
Domain PASSES!
============================
Testing: mail.yahoo.net
Domain PASSES!
============================
Testing: some.other.domain.museum
Domain PASSES!
============================
Testing: failed.domain.wa
Domain FAILS!
============================
Testing: this-domain-name-will-fail-because-there-are-too-many-characters-in-one-node.com
Domain FAILS!
============================
Testing: this-domain-name.will-pass-because-the.domain-name.is-way-too-long.but-is-under.255.characters.and-this-domain-name-is-165-characters.long-which-is-way-too-long.aero
Domain PASSES!
============================
*/
?>

Example 2: Verifying a domain name

<?php

include('domains.php');

$domains = new Domains;

if($domains->validateDomain('blogchuck.com', true))
{
	echo "Domain PASSES!";
}else{
	echo "Domain FAILS!";
}

?>

Example 3: Validate IP Address filtering for private

<?php

include('domains.php');

$domains = new Domains;

if($domains->validateIP('192.168.0.1', 'private'))
{
	echo "IP PASSES!";
}else{
	echo "IP FAILS!";
}

// expected output
/*
IP FAILS!
*/

?>

About

Domain names are an important part of the Internet. Without them (and the DNS), we would have to remember IP addresses. Who wants to visit websites called 192.168.0.1 anyway? What a pain that would be. A domain name is designed to reference a server by a name rather than a series of numbers separated by dots. However, there are rules as to how these names are formed. This class will follow those rules and allow you to validate the domain name either for a server you think might exists or for the domain portion of an email address.

This class will validate any domain name passed to it using RFC compliance to validate the domain name. There are few validations that still need to be confirmed. For example, RFC 1034 section 3.1 indicates that brother nodes may not have the same label, although the same label can be used for nodes which are not brothers. However, when I visit host.host.com, the domain resolves just fine. Therefore, my class will validate host.host.com So I need to find out if this has been changed in an RFC somewhere. If you know, please feel free to comment.

Summary

Basic rules of the domain validation

  • must be at least one character long
  • must start with a letter or number
  • contains letters, numbers, and hyphens
  • must end in a letter or number
  • may contain multiple nodes (i.e. node1.node2.node3)
  • each node can only be 63 characters long max
  • total domain name can only be 255 characters long max
  • must end in a valid TLD
  • can be an IP4 address

The validation works in the following order:

  • Checks the length of the domain to make sure it is less than 256 characters long but more than 1.
  • Splits the domain into nodes.
  • Checks each node to make sure length is less than 63 characters in length.
  • Check to make sure the node has at least 1 valid character. Starts with a number or letter, has numbers/letters/hyphens in the middle, and a number or letter on the end.
  • Verifies the last node is a valid TLD

TLD The TLD list is read from a file on the local hard drive. Each time the validate domain is called, a private class is executed to check the file creation/modified date of the TLD file. If it is more than 30 days old (or it does not exist), it will pull a new copy from iana.org (http://data.iana.org/TLD/tlds-alpha-by-domain.txt). As long as a copy of this file exists on the server and is accessible by the this class file, the validation process will work correctly.

When this class reads the TLD file, it will ignore any row that has non-word values in it. This means it will remove the header at the top that identifies when the file was generated and all of the XN-- value TLDs. This XN-- domains are test TLDs anyway for double-byte characters for foreign language URLs.

RFC References

RFC 5321 "Simple Mail Transfer Protocol" 2008

4.5.3.1.2.  Domain
 - The maximum total length of a domain name or number is 255 octets.

RFC 3696 "Application Techniques for Checking and Transformation of Names" 2004
 (http://www.rfc-editor.org/rfc/rfc3696.txt)

2. Restrictions on domain (DNS) names
 - IANA [DomainList] 
	 - http://www.iana.org/domains/root/db/
	 - http://data.iana.org/TLD/tlds-alpha-by-domain.txt
 - If the hyphen is used, it is not permitted to appear at either the beginning or end of a label.
 - Consequently, purported DNS names to be used in applications and to locate resources generally must contain at least one period (".") character.
 - label may be no more than 63 octets long
 - FQDN must not exceed 255 octets

3. Restrictions on email addresses
 - The domain name can also be replaced by an IP address in square brackets, but that form is strongly discouraged except for
   testing and troubleshooting purposes.

RFC 1035 " DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION" 1987
 (http://www.ietf.org/rfc/rfc1035.txt)
  
2.3.1
 - DEF: label  = <letter> [interior characters] <letter|digit>
 - any of 52 alphabetic characters (a-zA-Z)
 - any of the ten digits
 - hyphen
 - MUST start with a letter
 - MUST end with a letter or digit
 - MUST have as interior charactors only letters, digits, and hyphen
 - labels must be 63 characters or less
 - can include multiple labels (aka subdomains) i.e. this.sub.domain.com

RFC 1123 "Requirements for Internet Hosts" 1989
 (http://www.ietf.org/rfc/rfc1123.txt)

2.1 Host Names and Numbers
 - first character is relaxed to allow either a letter or a digit
 - MUST handle host names of up to 63 characters
 - SHOULD handle host names of up to 255 characters
 - SHOULD be possible to enter either (1) a host domain name or (2) an IP address in dotted-decimal ("#.#.#.#") form
  
RFC 1034 "DOMAIN NAMES - CONCEPTS AND FACILITIES" 1987
 (http://www.ietf.org/rfc/rfc1034.txt)

3.1 Name space specifications and terminology
 - Each node has a label, which is zero to 63 octets in length.
 - Brother nodes may not have the same label, although the same label can be used for nodes which are not brothers.
 - The domain name of a node is the list of the labels on the path from the node to the root of the tree.
 - domain functions are done in a case-insensitive manner
 - labels are separated by dots (".").
 - the total number of octets that represent a domain name (i.e., the sum of all label octets and label lengths) is limited to 255

3.5. Preferred name syntax
 - Note that while upper and lower case letters are allowed in domain names, no significance is attached to the case.
 - They must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen.
 - Labels must be 63 characters or less.
Comment by uto...@gmail.com, Jul 24, 2010

Re: Brother Nodes

In your example host.host.com, the two 'host' nodes are not brothers. One is the child of the other.

Brother nodes are both children of the same parent. So, in the following example, host1 and host2 are brothers:

host1.com host2.com

So, you can't have two domains that are exactly the same. Duh. :)


Sign in to add a comment
Powered by Google Project Hosting