Zend_Validate-Hostname.xml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. Zend_Validate_Hostname allows you to validate a hostname against a set of known specifications.
  7. It is possible to check for three different types of hostnames: a DNS Hostname (i.e.
  8. domain.com), IP address (i.e. 1.2.3.4), and Local hostnames (i.e. localhost). By default only
  9. DNS hostnames are matched.
  10. </para>
  11. <para>
  12. <emphasis role="strong">Basic usage</emphasis>
  13. </para>
  14. <para>
  15. A basic example of usage is below:
  16. <programlisting role="php"><![CDATA[
  17. $validator = new Zend_Validate_Hostname();
  18. if ($validator->isValid($hostname)) {
  19. // hostname appears to be valid
  20. } else {
  21. // hostname is invalid; print the reasons
  22. foreach ($validator->getMessages() as $message) {
  23. echo "$message\n";
  24. }
  25. }
  26. ]]></programlisting>
  27. This will match the hostname <code>$hostname</code> and on failure populate
  28. <code>$validator->getMessages()</code> with useful error messages.
  29. </para>
  30. <para>
  31. <emphasis role="strong">Validating different types of hostnames</emphasis>
  32. </para>
  33. <para>
  34. You may find you also want to match IP addresses, Local hostnames, or a combination of all
  35. allowed types. This can be done by passing a parameter to Zend_Validate_Hostname when you
  36. instantiate it. The parameter should be an integer which determines what types of hostnames
  37. are allowed. You are encouraged to use the Zend_Validate_Hostname constants to do this.
  38. </para>
  39. <para>
  40. The Zend_Validate_Hostname constants are: <code>ALLOW_DNS</code> to allow only DNS hostnames,
  41. <code>ALLOW_IP</code> to allow IP addresses, <code>ALLOW_LOCAL</code> to allow local network
  42. names, and <code>ALLOW_ALL</code> to allow all three types. To just check for IP addresses
  43. you can use the example below:
  44. <programlisting role="php"><![CDATA[
  45. $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_IP);
  46. if ($validator->isValid($hostname)) {
  47. // hostname appears to be valid
  48. } else {
  49. // hostname is invalid; print the reasons
  50. foreach ($validator->getMessages() as $message) {
  51. echo "$message\n";
  52. }
  53. }
  54. ]]></programlisting>
  55. </para>
  56. <para>
  57. As well as using <code>ALLOW_ALL</code> to accept all hostnames types you can combine
  58. these types to allow for combinations. For example, to accept DNS and Local hostnames
  59. instantiate your Zend_Validate_Hostname object as so:
  60. <programlisting role="php"><![CDATA[
  61. $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS |
  62. Zend_Validate_Hostname::ALLOW_IP);
  63. ]]></programlisting>
  64. </para>
  65. <para>
  66. <emphasis role="strong">Validating International Domains Names</emphasis>
  67. </para>
  68. <para>
  69. Some Country Code Top Level Domains (ccTLDs), such as 'de' (Germany), support international
  70. characters in domain names. These are known as International Domain Names (IDN). These domains
  71. can be matched by Zend_Validate_Hostname via extended characters that are used in the validation
  72. process.
  73. </para>
  74. <para>
  75. At present the list of supported ccTLDs include:
  76. <itemizedlist>
  77. <listitem>
  78. <para>at (Austria)</para>
  79. </listitem>
  80. <listitem>
  81. <para>ch (Switzerland)</para>
  82. </listitem>
  83. <listitem>
  84. <para>li (Liechtenstein)</para>
  85. </listitem>
  86. <listitem>
  87. <para>de (Germany)</para>
  88. </listitem>
  89. <listitem>
  90. <para>fi (Finland)</para>
  91. </listitem>
  92. <listitem>
  93. <para>hu (Hungary)</para>
  94. </listitem>
  95. <listitem>
  96. <para>no (Norway)</para>
  97. </listitem>
  98. <listitem>
  99. <para>se (Sweden)</para>
  100. </listitem>
  101. </itemizedlist>
  102. </para>
  103. <para>
  104. To match an IDN domain it's as simple as just using the standard Hostname validator since IDN
  105. matching is enabled by default. If you wish to disable IDN validation this can be done by
  106. by either passing a parameter to the Zend_Validate_Hostname constructor or via the
  107. <code>$validator->setValidateIdn()</code> method.
  108. </para>
  109. <para>
  110. You can disable IDN validation by passing a second parameter to the Zend_Validate_Hostname
  111. constructor in the following way.
  112. <programlisting role="php"><![CDATA[
  113. $validator =
  114. new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS, false);
  115. ]]></programlisting>
  116. Alternatively you can either pass TRUE or FALSE to
  117. <code>$validator->setValidateIdn()</code> to enable or disable IDN validation.
  118. If you are trying to match an IDN hostname which isn't currently supported it is likely
  119. it will fail validation if it has any international characters in it. Where a ccTLD file
  120. doesn't exist in Zend/Validate/Hostname specifying the additional characters a normal
  121. hostname validation is performed.
  122. </para>
  123. <para>
  124. Please note IDNs are only validated if you allow DNS hostnames to be validated.
  125. </para>
  126. <para>
  127. <emphasis role="strong">Validating Top Level Domains</emphasis>
  128. </para>
  129. <para>
  130. By default a hostname will be checked against a list of known TLDs. If this functionality
  131. is not required it can be disabled in much the same way as disabling IDN support.
  132. You can disable TLD validation by passing a third parameter to the Zend_Validate_Hostname constructor.
  133. In the example below we are supporting IDN validation via the second parameter.
  134. <programlisting role="php"><![CDATA[
  135. $validator =
  136. new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS,
  137. true,
  138. false);
  139. ]]></programlisting>
  140. Alternatively you can either pass TRUE or FALSE to
  141. <code>$validator->setValidateTld()</code> to enable or disable TLD validation.
  142. </para>
  143. <para>
  144. Please note TLDs are only validated if you allow DNS hostnames to be validated.
  145. </para>
  146. </sect2>
  147. <!--
  148. vim:se ts=4 sw=4 et:
  149. -->