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
  7. set of known specifications. It is possible to check for three different types of hostnames:
  8. a DNS Hostname (i.e. domain.com), IP address (i.e. 1.2.3.4), and Local hostnames (i.e.
  9. localhost). 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
  36. <classname>Zend_Validate_Hostname</classname> when you instantiate it. The parameter should
  37. be an integer which determines what types of hostnames are allowed. You are encouraged to
  38. use the <classname>Zend_Validate_Hostname</classname> constants to do this.
  39. </para>
  40. <para>
  41. The Zend_Validate_Hostname constants are: <constant>ALLOW_DNS</constant> to allow only DNS
  42. hostnames, <constant>ALLOW_IP</constant> to allow IP addresses,
  43. <constant>ALLOW_LOCAL</constant> to allow local network names, and
  44. <constant>ALLOW_ALL</constant> to allow all three types. To just check for IP addresses you
  45. can use the example below:
  46. <programlisting language="php"><![CDATA[
  47. $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_IP);
  48. if ($validator->isValid($hostname)) {
  49. // hostname appears to be valid
  50. } else {
  51. // hostname is invalid; print the reasons
  52. foreach ($validator->getMessages() as $message) {
  53. echo "$message\n";
  54. }
  55. }
  56. ]]></programlisting>
  57. </para>
  58. <para>
  59. As well as using <constant>ALLOW_ALL</constant> to accept all hostnames types you can
  60. combine these types to allow for combinations. For example, to accept DNS and Local
  61. hostnames instantiate your Zend_Validate_Hostname object as so:
  62. <programlisting language="php"><![CDATA[
  63. $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS |
  64. Zend_Validate_Hostname::ALLOW_IP);
  65. ]]></programlisting>
  66. </para>
  67. <para>
  68. <emphasis>Validating International Domains Names</emphasis>
  69. </para>
  70. <para>
  71. Some Country Code Top Level Domains (ccTLDs), such as 'de' (Germany), support international
  72. characters in domain names. These are known as International Domain Names (IDN). These
  73. domains can be matched by <classname>Zend_Validate_Hostname</classname> via extended
  74. characters that are used in the validation process.
  75. </para>
  76. <para>
  77. Until now more than 50 ccTLDs support IDN domains.
  78. </para>
  79. <para>
  80. To match an IDN domain it's as simple as just using the standard Hostname validator since
  81. IDN matching is enabled by default. If you wish to disable IDN validation this can be done
  82. by either passing a parameter to the <classname>Zend_Validate_Hostname</classname>
  83. constructor or via the <methodname>setValidateIdn()</methodname> method.
  84. </para>
  85. <para>
  86. You can disable IDN validation by passing a second parameter to the Zend_Validate_Hostname
  87. constructor in the following way.
  88. <programlisting language="php"><![CDATA[
  89. $validator =
  90. new Zend_Validate_Hostname(
  91. array(
  92. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  93. 'idn' => false
  94. )
  95. );
  96. ]]></programlisting>
  97. Alternatively you can either pass <constant>TRUE</constant> or <constant>FALSE</constant> to
  98. <methodname>setValidateIdn()</methodname> to enable or disable IDN validation.
  99. If you are trying to match an IDN hostname which isn't currently supported it is likely
  100. it will fail validation if it has any international characters in it. Where a ccTLD file
  101. doesn't exist in Zend/Validate/Hostname specifying the additional characters a normal
  102. hostname validation is performed.
  103. </para>
  104. <para>
  105. Please note IDNs are only validated if you allow DNS hostnames to be validated.
  106. </para>
  107. <para>
  108. <emphasis>Validating Top Level Domains</emphasis>
  109. </para>
  110. <para>
  111. By default a hostname will be checked against a list of known TLDs. If this functionality
  112. is not required it can be disabled in much the same way as disabling IDN support. You can
  113. disable TLD validation by passing a third parameter to the Zend_Validate_Hostname
  114. constructor. In the example below we are supporting IDN validation via the second parameter.
  115. <programlisting language="php"><![CDATA[
  116. $validator =
  117. new Zend_Validate_Hostname(
  118. array(
  119. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  120. 'idn' => true,
  121. 'tld' => false
  122. )
  123. );
  124. ]]></programlisting>
  125. Alternatively you can either pass <constant>TRUE</constant> or <constant>FALSE</constant> to
  126. <methodname>setValidateTld()</methodname> to enable or disable TLD validation.
  127. </para>
  128. <para>
  129. Please note TLDs are only validated if you allow DNS hostnames to be validated.
  130. </para>
  131. </sect2>
  132. <!--
  133. vim:se ts=4 sw=4 et:
  134. -->