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.
blog comments powered by Disqus |
|
|
|
|
|
|
|