Zend_Validate-Hostname.xml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 set of known
  7. specifications. It is possible to check for three different types of hostnames: a DNS
  8. Hostname (i.e. domain.com), IP address (i.e. 1.2.3.4), and Local hostnames (i.e. localhost).
  9. By default only DNS hostnames are matched.
  10. </para>
  11. <para>
  12. <emphasis>Basic usage</emphasis>
  13. </para>
  14. <para>
  15. A basic example of usage is below:
  16. <programlisting language="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 <varname>$hostname</varname> and on failure populate
  28. <methodname>getMessages()</methodname> with useful error messages.
  29. </para>
  30. <para>
  31. <emphasis>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 <classname>Zend_Validate_Hostname</classname> 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 <classname>Zend_Validate_Hostname</classname> constants to do this.
  38. </para>
  39. <para>
  40. The Zend_Validate_Hostname constants are: <constant>ALLOW_DNS</constant> to allow only DNS
  41. hostnames, <constant>ALLOW_IP</constant> to allow IP addresses, <constant>ALLOW_LOCAL</constant> to allow
  42. local network names, and <constant>ALLOW_ALL</constant> to allow all three types. To just check for
  43. IP addresses you can use the example below:
  44. <programlisting language="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 <constant>ALLOW_ALL</constant> 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 language="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>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
  71. domains can be matched by <classname>Zend_Validate_Hostname</classname> via extended characters that are used in
  72. the validation process.
  73. </para>
  74. <para>
  75. Until now more than 50 ccTLDs support IDN domains.
  76. </para>
  77. <para>
  78. To match an IDN domain it's as simple as just using the standard Hostname validator since
  79. IDN matching is enabled by default. If you wish to disable IDN validation this can be done
  80. by either passing a parameter to the <classname>Zend_Validate_Hostname</classname>
  81. constructor or via the <methodname>setValidateIdn()</methodname> method.
  82. </para>
  83. <para>
  84. You can disable IDN validation by passing a second parameter to the Zend_Validate_Hostname
  85. constructor in the following way.
  86. <programlisting language="php"><![CDATA[
  87. $validator =
  88. new Zend_Validate_Hostname(
  89. array(
  90. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  91. 'idn' => false
  92. )
  93. );
  94. ]]></programlisting>
  95. Alternatively you can either pass <constant>TRUE</constant> or <constant>FALSE</constant> to
  96. <methodname>setValidateIdn()</methodname> to enable or disable IDN validation.
  97. If you are trying to match an IDN hostname which isn't currently supported it is likely
  98. it will fail validation if it has any international characters in it. Where a ccTLD file
  99. doesn't exist in Zend/Validate/Hostname specifying the additional characters a normal
  100. hostname validation is performed.
  101. </para>
  102. <para>
  103. Please note IDNs are only validated if you allow DNS hostnames to be validated.
  104. </para>
  105. <para>
  106. <emphasis>Validating Top Level Domains</emphasis>
  107. </para>
  108. <para>
  109. By default a hostname will be checked against a list of known TLDs. If this functionality
  110. is not required it can be disabled in much the same way as disabling IDN support. You can
  111. disable TLD validation by passing a third parameter to the Zend_Validate_Hostname
  112. constructor. In the example below we are supporting IDN validation via the second parameter.
  113. <programlisting language="php"><![CDATA[
  114. $validator =
  115. new Zend_Validate_Hostname(
  116. array(
  117. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  118. 'idn' => true,
  119. 'tld' => false
  120. )
  121. );
  122. ]]></programlisting>
  123. Alternatively you can either pass <constant>TRUE</constant> or <constant>FALSE</constant> to
  124. <methodname>setValidateTld()</methodname> to enable or disable TLD validation.
  125. </para>
  126. <para>
  127. Please note TLDs are only validated if you allow DNS hostnames to be validated.
  128. </para>
  129. </sect2>
  130. <!--
  131. vim:se ts=4 sw=4 et:
  132. -->