Home arrow PHP arrow Page 3 - Validating IP Addresses with Filters in PHP 5

Introducing the FILTER_VALIDATE_IP filter - PHP

If you’re a PHP developer who wishes to learn how to take advantage of the solid functionality provided by the filter extension that comes with PHP 5, then look no further, because you’ve come to the right place. Welcome to the seventh part of a nine-part series that gets you started using the most useful validation filters included with this PHP library.

TABLE OF CONTENTS:
  1. Validating IP Addresses with Filters in PHP 5
  2. Review: the FILTER_VALIDATE_URL filter
  3. Introducing the FILTER_VALIDATE_IP filter
  4. Checking for IPv6-compliant addresses
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
August 26, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In reality, checking for well-formatted IP addresses with the filter extension is a process reduced to using a brand new filter, not surprisingly called FILTER_VALIDATE_IP. As you'll see in a moment, it allows you to not only determine if a specified IP address is valid, but permits you to perform this process utilizing both IPv4 and IPv6 protocols.

But first, let me show you how to do a basic validation on a badly-formatted IP address by means of the aforementioned filter. Take a look at the following example, please:

 

// example on validating IP address

$ip = '192.168.37';

if(filter_var($ip, FILTER_VALIDATE_IP) === FALSE) // displays IP is valid.

{

echo 'IP is not valid.';

}

else

{

echo 'IP is valid.';

}

That was pretty simple to grasp, wasn't it? As shown above, the "filter_var()" function has been used in conjunction with the FILTER_VALIDATE_IP filter to validate an incorrect IP address. The example itself is rather trivial, but it shows in a nutshell how to accomplish this task very quickly.

Moving forward, imagine for a moment that you need to determine if an IP address sticks to the IPv4 protocol. In that case, it's time to use an additional argument called FILTER_FLAG_IPV4, which must be specified in the following way:

// example on validating IP address using the FILTER_FLAG_IPV4 argument

$ip = '192.168.37';

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) // displays IP is not valid

{

echo 'IP is not valid.';

}

else

{

echo 'IP is valid.';

}

 

Again, I decided to keep the above example very simple, so you can understand how the FILTER_FLAG_IPV4 option works. In this case, the supplied IP address has been incorrectly formatted on purpose, but you may want to create more complex scripts that check for IPv4-compliant IP addresses.

All in all, at this stage you hopefully grasped the logic that stands behind validating IPv4 addresses. However, as you know the Internet is running out of available IP addresses due to its tremendous growth.

Therefore, in the last section of this article I'm going to show how to use the FILTER_VALIDATE_IP filter for validating IPv6-compliant and private IP addresses. As usual, to see how this will be done, click on the link below and read the segment to come.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: