Zend_Validate-Hostname.xml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 Zend_Validate_Hostname constants are: <constant>ALLOW_DNS</constant> to allow only
  80. <acronym>DNS</acronym> hostnames, <constant>ALLOW_IP</constant> to allow IP addresses,
  81. <constant>ALLOW_LOCAL</constant> to allow local network names, and
  82. <constant>ALLOW_ALL</constant> to allow all three types. To just check for IP addresses
  83. you can use the example below:
  84. </para>
  85. <programlisting language="php"><![CDATA[
  86. $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_IP);
  87. if ($validator->isValid($hostname)) {
  88. // hostname appears to be valid
  89. } else {
  90. // hostname is invalid; print the reasons
  91. foreach ($validator->getMessages() as $message) {
  92. echo "$message\n";
  93. }
  94. }
  95. ]]></programlisting>
  96. <para>
  97. As well as using <constant>ALLOW_ALL</constant> to accept all hostnames types you can
  98. combine these types to allow for combinations. For example, to accept DNS and Local
  99. hostnames instantiate your Zend_Validate_Hostname object as so:
  100. </para>
  101. <programlisting language="php"><![CDATA[
  102. $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS |
  103. Zend_Validate_Hostname::ALLOW_IP);
  104. ]]></programlisting>
  105. </sect3>
  106. <sect3 id="zend.validate.set.hostname.idn">
  107. <title>Validating International Domains Names</title>
  108. <para>
  109. Some Country Code Top Level Domains (ccTLDs), such as 'de' (Germany), support
  110. international characters in domain names. These are known as International Domain Names
  111. (<acronym>IDN</acronym>). These domains can be matched by
  112. <classname>Zend_Validate_Hostname</classname> via extended characters that are used in
  113. the validation process.
  114. </para>
  115. <note>
  116. <title>IDN domains</title>
  117. <para>
  118. Until now more than 50 ccTLDs support IDN domains.
  119. </para>
  120. </note>
  121. <para>
  122. To match an <acronym>IDN</acronym> domain it's as simple as just using the standard
  123. Hostname validator since <acronym>IDN</acronym> matching is enabled by default. If you
  124. wish to disable <acronym>IDN</acronym> validation this can be done by either passing a
  125. parameter to the <classname>Zend_Validate_Hostname</classname> constructor or via the
  126. <methodname>setValidateIdn()</methodname> method.
  127. </para>
  128. <para>
  129. You can disable <acronym>IDN</acronym> validation by passing a second parameter to the
  130. <classname>Zend_Validate_Hostname</classname> constructor in the following way.
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. $validator =
  134. new Zend_Validate_Hostname(
  135. array(
  136. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  137. 'idn' => false
  138. )
  139. );
  140. ]]></programlisting>
  141. <para>
  142. Alternatively you can either pass <constant>TRUE</constant> or
  143. <constant>FALSE</constant> to <methodname>setValidateIdn()</methodname> to enable or
  144. disable <acronym>IDN</acronym> validation. If you are trying to match an
  145. <acronym>IDN</acronym> hostname which isn't currently supported it is likely it will
  146. fail validation if it has any international characters in it. Where a ccTLD file doesn't
  147. exist in <filename>Zend/Validate/Hostname</filename> specifying the additional
  148. characters a normal hostname validation is performed.
  149. </para>
  150. <note>
  151. <title>IDN validation</title>
  152. <para>
  153. Please note that <acronym>IDN</acronym>s are only validated if you allow
  154. <acronym>DNS</acronym> hostnames to be validated.
  155. </para>
  156. </note>
  157. </sect3>
  158. <sect3 id="zend.validate.set.hostname.tld">
  159. <title>Validating Top Level Domains</title>
  160. <para>
  161. By default a hostname will be checked against a list of known <acronym>TLD</acronym>s.
  162. If this functionality is not required it can be disabled in much the same way as
  163. disabling <acronym>IDN</acronym> support. You can disable <acronym>TLD</acronym>
  164. validation by passing a third parameter to the
  165. <classname>Zend_Validate_Hostname</classname> constructor. In the example below we are
  166. supporting <acronym>IDN</acronym> validation via the second parameter.
  167. </para>
  168. <programlisting language="php"><![CDATA[
  169. $validator =
  170. new Zend_Validate_Hostname(
  171. array(
  172. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  173. 'idn' => true,
  174. 'tld' => false
  175. )
  176. );
  177. ]]></programlisting>
  178. <para>
  179. Alternatively you can either pass <constant>TRUE</constant> or
  180. <constant>FALSE</constant> to <methodname>setValidateTld()</methodname> to enable or
  181. disable <acronym>TLD</acronym> validation.
  182. </para>
  183. <note>
  184. <title>TLD validation</title>
  185. <para>
  186. Please note <acronym>TLD</acronym>s are only validated if you allow
  187. <acronym>DNS</acronym> hostnames to be validated.
  188. </para>
  189. </note>
  190. </sect3>
  191. </sect2>
  192. <!--
  193. vim:se ts=4 sw=4 et:
  194. -->