Zend_Validate-Hostname.xml 9.5 KB

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