Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store


Zend_Validate_Ip allows you to validate if a given value is an IP address. It supports the IPv4 and also the IPv6 standard.

Supported options for Zend_Validate_Ip

The following options are supported for Zend_Validate_Ip:

  • allowipv4: Defines if the validator allows IPv4 adresses. This option defaults to TRUE.

  • allowipv6: Defines if the validator allows IPv6 adresses. This option defaults to TRUE.

Basic usage

A basic example of usage is below:

= new Zend_Validate_Ip();
if (
$validator->isValid($ip)) {
// ip appears to be valid
} else {
// ip is invalid; print the reasons

Invalid IP addresses

Keep in mind that Zend_Validate_Ip only validates IP addresses. Addresses like '' or '' are no valid IP addresses. They are either hostnames or valid URLs but not IP addresses.

IPv6 validation

Zend_Validate_Ip validates IPv6 addresses with regex. The reason is that the filters and methods from PHP itself don't follow the RFC. Many other available classes also don't follow it.

Validate IPv4 or IPV6 alone

Sometimes it's useful to validate only one of the supported formats. For example when your network only supports IPv4. In this case it would be useless to allow IPv6 within this validator.

To limit Zend_Validate_Ip to one protocol you can set the options allowipv4 or allowipv6 to FALSE. You can do this either by giving the option to the constructor or by using setOptions() afterwards.

= new Zend_Validate_Ip(array('allowipv6' => false);
if (
$validator->isValid($ip)) {
// ip appears to be valid ipv4 address
} else {
// ip is no ipv4 address

Default behaviour

The default behaviour which Zend_Validate_Ip follows is to allow both standards.

Zend Framework