Zend_Validate-Hostname.xml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.hostname">
  4. <title>Hostname</title>
  5. <para>
  6. <classname>Zend_Validate_Hostname</classname> allows you to validate a hostname against a
  7. set of known specifications. It is possible to check for three different types of hostnames:
  8. a DNS Hostname (i.e. domain.com), IP address (i.e. 1.2.3.4), and Local hostnames (i.e.
  9. localhost). By default only DNS hostnames are matched.
  10. </para>
  11. <sect3 id="zend.validate.set.hostname.options">
  12. <title>Supported options for Zend_Validate_Hostname</title>
  13. <para>
  14. The following options are supported for <classname>Zend_Validate_Hostname</classname>:
  15. </para>
  16. <itemizedlist>
  17. <listitem>
  18. <para>
  19. <emphasis><property>allow</property></emphasis>: Defines the sort of hostname
  20. which is allowed to be used. See <link
  21. linkend="zend.validate.set.hostname.types">Hostname types</link> for
  22. details.
  23. </para>
  24. </listitem>
  25. <listitem>
  26. <para>
  27. <emphasis><property>idn</property></emphasis>: Defines if <acronym>IDN</acronym>
  28. domains are allowed or not. This option defaults to <constant>TRUE</constant>.
  29. </para>
  30. </listitem>
  31. <listitem>
  32. <para>
  33. <emphasis><property>ip</property></emphasis>: Allows to define a own IP
  34. validator. This option defaults to a new instance of
  35. <classname>Zend_Validate_Ip</classname>.
  36. </para>
  37. </listitem>
  38. <listitem>
  39. <para>
  40. <emphasis><property>tld</property></emphasis>: Defines if
  41. <acronym>TLD</acronym>s are validated. This option defaults to
  42. <constant>TRUE</constant>.
  43. </para>
  44. </listitem>
  45. </itemizedlist>
  46. </sect3>
  47. <sect3 id="zend.validate.set.hostname.basic">
  48. <title>Basic usage</title>
  49. <para>
  50. A basic example of usage is below:
  51. </para>
  52. <programlisting language="php"><![CDATA[
  53. $validator = new Zend_Validate_Hostname();
  54. if ($validator->isValid($hostname)) {
  55. // hostname appears to be valid
  56. } else {
  57. // hostname is invalid; print the reasons
  58. foreach ($validator->getMessages() as $message) {
  59. echo "$message\n";
  60. }
  61. }
  62. ]]></programlisting>
  63. <para>
  64. This will match the hostname <varname>$hostname</varname> and on failure populate
  65. <methodname>getMessages()</methodname> with useful error messages.
  66. </para>
  67. </sect3>
  68. <sect3 id="zend.validate.set.hostname.types">
  69. <title>Validating different types of hostnames</title>
  70. <para>
  71. You may find you also want to match IP addresses, Local hostnames, or a combination of
  72. all allowed types. This can be done by passing a parameter to
  73. <classname>Zend_Validate_Hostname</classname> when you instantiate it. The parameter
  74. should be an integer which determines what types of hostnames are allowed. You are
  75. encouraged to use the <classname>Zend_Validate_Hostname</classname> constants to do
  76. this.
  77. </para>
  78. <para>
  79. The <classname>Zend_Validate_Hostname</classname> constants are:
  80. <constant>ALLOW_DNS</constant> to allow only
  81. <acronym>DNS</acronym> hostnames, <constant>ALLOW_IP</constant> to allow IP addresses,
  82. <constant>ALLOW_LOCAL</constant> to allow local network names, and
  83. <constant>ALLOW_ALL</constant> to allow all three types. To just check for IP addresses
  84. you can use the example below:
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_IP);
  88. if ($validator->isValid($hostname)) {
  89. // hostname appears to be valid
  90. } else {
  91. // hostname is invalid; print the reasons
  92. foreach ($validator->getMessages() as $message) {
  93. echo "$message\n";
  94. }
  95. }
  96. ]]></programlisting>
  97. <para>
  98. As well as using <constant>ALLOW_ALL</constant> to accept all hostnames types you can
  99. combine these types to allow for combinations. For example, to accept DNS and Local
  100. hostnames instantiate your <classname>Zend_Validate_Hostname</classname> object as so:
  101. </para>
  102. <programlisting language="php"><![CDATA[
  103. $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS |
  104. Zend_Validate_Hostname::ALLOW_IP);
  105. ]]></programlisting>
  106. </sect3>
  107. <sect3 id="zend.validate.set.hostname.idn">
  108. <title>Validating International Domains Names</title>
  109. <para>
  110. Some Country Code Top Level Domains (ccTLDs), such as 'de' (Germany), support
  111. international characters in domain names. These are known as International Domain Names
  112. (<acronym>IDN</acronym>). These domains can be matched by
  113. <classname>Zend_Validate_Hostname</classname> via extended characters that are used in
  114. the validation process.
  115. </para>
  116. <note>
  117. <title>IDN domains</title>
  118. <para>
  119. Until now more than 50 ccTLDs support IDN domains.
  120. </para>
  121. </note>
  122. <para>
  123. To match an <acronym>IDN</acronym> domain it's as simple as just using the standard
  124. Hostname validator since <acronym>IDN</acronym> matching is enabled by default. If you
  125. wish to disable <acronym>IDN</acronym> validation this can be done by either passing a
  126. parameter to the <classname>Zend_Validate_Hostname</classname> constructor or via the
  127. <methodname>setValidateIdn()</methodname> method.
  128. </para>
  129. <para>
  130. You can disable <acronym>IDN</acronym> validation by passing a second parameter to the
  131. <classname>Zend_Validate_Hostname</classname> constructor in the following way.
  132. </para>
  133. <programlisting language="php"><![CDATA[
  134. $validator =
  135. new Zend_Validate_Hostname(
  136. array(
  137. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  138. 'idn' => false
  139. )
  140. );
  141. ]]></programlisting>
  142. <para>
  143. Alternatively you can either pass <constant>TRUE</constant> or
  144. <constant>FALSE</constant> to <methodname>setValidateIdn()</methodname> to enable or
  145. disable <acronym>IDN</acronym> validation. If you are trying to match an
  146. <acronym>IDN</acronym> hostname which isn't currently supported it is likely it will
  147. fail validation if it has any international characters in it. Where a ccTLD file doesn't
  148. exist in <filename>Zend/Validate/Hostname</filename> specifying the additional
  149. characters a normal hostname validation is performed.
  150. </para>
  151. <note>
  152. <title>IDN validation</title>
  153. <para>
  154. Please note that <acronym>IDN</acronym>s are only validated if you allow
  155. <acronym>DNS</acronym> hostnames to be validated.
  156. </para>
  157. </note>
  158. </sect3>
  159. <sect3 id="zend.validate.set.hostname.tld">
  160. <title>Validating Top Level Domains</title>
  161. <para>
  162. By default a hostname will be checked against a list of known <acronym>TLD</acronym>s.
  163. If this functionality is not required it can be disabled in much the same way as
  164. disabling <acronym>IDN</acronym> support. You can disable <acronym>TLD</acronym>
  165. validation by passing a third parameter to the
  166. <classname>Zend_Validate_Hostname</classname> constructor. In the example below we are
  167. supporting <acronym>IDN</acronym> validation via the second parameter.
  168. </para>
  169. <programlisting language="php"><![CDATA[
  170. $validator =
  171. new Zend_Validate_Hostname(
  172. array(
  173. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  174. 'idn' => true,
  175. 'tld' => false
  176. )
  177. );
  178. ]]></programlisting>
  179. <para>
  180. Alternatively you can either pass <constant>TRUE</constant> or
  181. <constant>FALSE</constant> to <methodname>setValidateTld()</methodname> to enable or
  182. disable <acronym>TLD</acronym> validation.
  183. </para>
  184. <note>
  185. <title>TLD validation</title>
  186. <para>
  187. Please note <acronym>TLD</acronym>s are only validated if you allow
  188. <acronym>DNS</acronym> hostnames to be validated.
  189. </para>
  190. </note>
  191. </sect3>
  192. </sect2>
  193. <!--
  194. vim:se ts=4 sw=4 et:
  195. -->