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

Review: the FILTER_VALIDATE_URL 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

Before I proceed to explain how to take advantage of the functionality provided by the filter extension to validate IP addresses, I'd like to reintroduce the group of code samples developed in the preceding tutorial. They demonstrated how easy it is to check different sections of a URL using the FILTER_VALIDATE_URL filter and its set of additional arguments.

That being said, here's how these examples looked originally. Pay close attention to them:

(example on validating URL's using the FILTER_FLAG_SCHEME_REQUIRED argument)

$url = 'index.php';

// validate URL

if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === FALSE) // displays The URL provided is not valid.

{

echo 'The URL provided is not valid.';

}

else

{

echo 'The URL provided is valid.';

}

(example on validating URL's using the FILTER_FLAG_ SCHEME_REQUIRED argument)

$url = 'http://devshed';

// validate URL

if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === FALSE) // displays The URL provided is valid.

{

echo 'The URL provided is not valid.';

}

else

{

echo 'The URL provided is valid.';

}

(example on validating URL's using the FILTER_FLAG_HOST_REQUIRED argument)

$url = 'http://devshed.com';

// validate URL

if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE) // displays The URL provided is valid.

{

echo 'The URL provided is not valid.';

}

else

{

echo 'The URL provided is valid.';

}

// example on validating URL's using the FILTER_FLAG_HOST_REQUIRED argument

$url = 'http://';

// validate URL

if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE) // displays The URL provided is not valid.

{

echo 'The URL provided is not valid.';

}

else

{

echo 'The URL provided is valid.';

}

(example on validating URL's using the FILTER_FLAG_PATH_REQUIRED argument)

$url = 'http://devshed.com/path/to/images/';

// validate URL

if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) === FALSE) // displays The URL provided is valid.

{

echo 'The URL provided is not valid.';

}

else

{

echo 'The URL provided is valid.';

}

 

 

(example on validating URL's using the FILTER_FLAG_QUERY_REQUIRED argument)

$url = 'http://devshed.com/?id=1';

// validate URL

if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === FALSE) // displays The URL provided is valid.

{

echo 'The URL provided is not valid.';

}

else

{

echo 'The URL provided is valid.';

}

From the code samples shown above, it's clear to see how easy it is to check if the different sections that compose a URL are valid, thanks to the assistance of the FILTER_VALIDATE_URL filter and its additional arguments. The first case shows how to check for the URL's schema, while the second and the third ones validate the host and path portions. Finally, the last example checks to see if the given URL contains a query string. It's all short and effective.

Well, now that you hopefully are pretty familiar with using the FILTER_VALIDATE_URL filter, it's time to explore a few more validation capabilities offered by the filter extension. With that idea in mind, in the following section I'm going to explain how to use it for validating IP addresses. You'll find this process very useful, trust me.

This topic will be discussed in detail in the next segment. Thus, to learn more about it, please click on the link below and keep reading.



 
 
>>> 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: