1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect2 id="zend.validate.set.hostname">
4 <title>Hostname</title>
7 <classname>Zend_Validate_Hostname</classname> allows you to validate a hostname against a
8 set of known specifications. It is possible to check for three different types of hostnames:
9 a DNS Hostname (i.e. domain.com), IP address (i.e. 1.2.3.4), and Local hostnames (i.e.
10 localhost). By default only DNS hostnames are matched.
13 <sect3 id="zend.validate.set.hostname.options">
14 <title>Supported options for Zend_Validate_Hostname</title>
17 The following options are supported for <classname>Zend_Validate_Hostname</classname>:
23 <emphasis><property>allow</property></emphasis>: Defines the sort of hostname
24 which is allowed to be used. See <link
25 linkend="zend.validate.set.hostname.types">Hostname types</link> for
32 <emphasis><property>idn</property></emphasis>: Defines if <acronym>IDN</acronym>
33 domains are allowed or not. This option defaults to <constant>TRUE</constant>.
39 <emphasis><property>ip</property></emphasis>: Allows to define a own IP
40 validator. This option defaults to a new instance of
41 <classname>Zend_Validate_Ip</classname>.
47 <emphasis><property>tld</property></emphasis>: Defines if
48 <acronym>TLD</acronym>s are validated. This option defaults to
49 <constant>TRUE</constant>.
55 <sect3 id="zend.validate.set.hostname.basic">
56 <title>Basic usage</title>
59 A basic example of usage is below:
62 <programlisting language="php"><![CDATA[
63 $validator = new Zend_Validate_Hostname();
64 if ($validator->isValid($hostname)) {
65 // hostname appears to be valid
67 // hostname is invalid; print the reasons
68 foreach ($validator->getMessages() as $message) {
75 This will match the hostname <varname>$hostname</varname> and on failure populate
76 <methodname>getMessages()</methodname> with useful error messages.
80 <sect3 id="zend.validate.set.hostname.types">
81 <title>Validating different types of hostnames</title>
84 You may find you also want to match IP addresses, Local hostnames, or a combination of
85 all allowed types. This can be done by passing a parameter to
86 <classname>Zend_Validate_Hostname</classname> when you instantiate it. The parameter
87 should be an integer which determines what types of hostnames are allowed. You are
88 encouraged to use the <classname>Zend_Validate_Hostname</classname> constants to do
93 The Zend_Validate_Hostname constants are: <constant>ALLOW_DNS</constant> to allow only
94 <acronym>DNS</acronym> hostnames, <constant>ALLOW_IP</constant> to allow IP addresses,
95 <constant>ALLOW_LOCAL</constant> to allow local network names, and
96 <constant>ALLOW_ALL</constant> to allow all three types. To just check for IP addresses
97 you can use the example below:
100 <programlisting language="php"><![CDATA[
101 $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_IP);
102 if ($validator->isValid($hostname)) {
103 // hostname appears to be valid
105 // hostname is invalid; print the reasons
106 foreach ($validator->getMessages() as $message) {
113 As well as using <constant>ALLOW_ALL</constant> to accept all hostnames types you can
114 combine these types to allow for combinations. For example, to accept DNS and Local
115 hostnames instantiate your Zend_Validate_Hostname object as so:
118 <programlisting language="php"><![CDATA[
119 $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS |
120 Zend_Validate_Hostname::ALLOW_IP);
124 <sect3 id="zend.validate.set.hostname.idn">
125 <title>Validating International Domains Names</title>
128 Some Country Code Top Level Domains (ccTLDs), such as 'de' (Germany), support
129 international characters in domain names. These are known as International Domain Names
130 (<acronym>IDN</acronym>). These domains can be matched by
131 <classname>Zend_Validate_Hostname</classname> via extended characters that are used in
132 the validation process.
136 <title>IDN domains</title>
139 Until now more than 50 ccTLDs support IDN domains.
144 To match an <acronym>IDN</acronym> domain it's as simple as just using the standard
145 Hostname validator since <acronym>IDN</acronym> matching is enabled by default. If you
146 wish to disable <acronym>IDN</acronym> validation this can be done by either passing a
147 parameter to the <classname>Zend_Validate_Hostname</classname> constructor or via the
148 <methodname>setValidateIdn()</methodname> method.
152 You can disable <acronym>IDN</acronym> validation by passing a second parameter to the
153 <classname>Zend_Validate_Hostname</classname> constructor in the following way.
156 <programlisting language="php"><![CDATA[
158 new Zend_Validate_Hostname(
160 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
167 Alternatively you can either pass <constant>TRUE</constant> or
168 <constant>FALSE</constant> to <methodname>setValidateIdn()</methodname> to enable or
169 disable <acronym>IDN</acronym> validation. If you are trying to match an
170 <acronym>IDN</acronym> hostname which isn't currently supported it is likely it will
171 fail validation if it has any international characters in it. Where a ccTLD file doesn't
172 exist in <filename>Zend/Validate/Hostname</filename> specifying the additional
173 characters a normal hostname validation is performed.
177 <title>IDN validation</title>
180 Please note that <acronym>IDN</acronym>s are only validated if you allow
181 <acronym>DNS</acronym> hostnames to be validated.
186 <sect3 id="zend.validate.set.hostname.tld">
187 <title>Validating Top Level Domains</title>
190 By default a hostname will be checked against a list of known <acronym>TLD</acronym>s.
191 If this functionality is not required it can be disabled in much the same way as
192 disabling <acronym>IDN</acronym> support. You can disable <acronym>TLD</acronym>
193 validation by passing a third parameter to the
194 <classname>Zend_Validate_Hostname</classname> constructor. In the example below we are
195 supporting <acronym>IDN</acronym> validation via the second parameter.
198 <programlisting language="php"><![CDATA[
200 new Zend_Validate_Hostname(
202 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
210 Alternatively you can either pass <constant>TRUE</constant> or
211 <constant>FALSE</constant> to <methodname>setValidateTld()</methodname> to enable or
212 disable <acronym>TLD</acronym> validation.
216 <title>TLD validation</title>
219 Please note <acronym>TLD</acronym>s are only validated if you allow
220 <acronym>DNS</acronym> hostnames to be validated.