Zend_Validate-Hostname.xml 9.4 KB

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