Home arrow PHP arrow Page 4 - Validating URL Protocols, Hosts and Paths with Filters in PHP 5

Validating paths within URLs: the FILTER_FLAG_PATH_REQUIRED argument - PHP

When it comes to checking incoming data in web applications, PHP 5 provides developers with a huge arsenal of functions that permit them to perform this process in a pretty straightforward way. Indeed, checking integers and float numbers, IP and email addresses, alphabetic strings and so forth is a process that must be tackled frequently by programmers. Fortunately, validating data types is not a huge production with PHP.

TABLE OF CONTENTS:
  1. Validating URL Protocols, Hosts and Paths with Filters in PHP 5
  2. Review: the FILTER_VALIDATE_URL filter
  3. Validating URLs more strictly with the FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED constants
  4. Validating paths within URLs: the FILTER_FLAG_PATH_REQUIRED argument
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
August 19, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Definitely, an appropriate epilogue for this article consists of showing how to use the FILTER_VALIDATE_URL filter for checking to see whether or not a given URL includes a path. Similar to the examples developed in the previous section, this validation process can be easily accomplished by means of another argument called FILTER_FLAG_PATH_REQUIRED, which must be passed to the “filter_var()” function that you learned before.

Having said that, here’s a basic example that shows how to work with the  argument when validating a simple URL. Have a look at it:

// example on validating URLS 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.';

}

 

As shown above, actually it’s pretty easy to determine if a URL contains a path portion by using the FILTER_FLAG_PATH_REQUIRED argument. In the example coded before, a fictional path has been specified for the first case, meaning that the “filter_var()” function will consider the inputted URL valid.

Moving forward, take a look at the following example, which shows how to determine if a URL contains a query string by way of another argument called FILTER_FLAG_QUERY_REQUIRED. Here it is:

// example on validating URLS 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.';

}

Finally, with this concluding code sample, I’m finishing this introduction to working with the FILTER_VALIDATE_URL filter. When used in conjunction with a few other handy arguments, as you saw it permits you to validate different sections of a URL, such as its host, path, query strings and so forth.

As always, feel free to edit all of the examples built in this tutorial, since doing so will help you to acquire a better background in working with the PHP 5 filter extension.

Final thoughts

Over the course of this sixth part of the series, I provided you with a quick overview of the different options available with the FILTER_VALIDATE_URL filter, which allows you to evaluate several sections of a specified URL and perform a more strict validation process on it.

In the upcoming article, I’m going to continue exploring the powerful capabilities given by the filter PHP 5 extension, this time by showing how to utilize it for validating IP addresses and sanitizing strings as well. Thus, here’s my final suggestion: don’t miss the next article!



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