Zend_Validate-Hostname.xml 8.9 KB

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