Validating URL Protocols, Hosts and Paths with Filters in PHP 5

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.

PHP 5 comes bundled with a useful set of native filters that allow you to validate all sorts of data types without having to spend a long time coding custom functions. These filters are known popularly as the filter extension and offer a flat learning curve. Therefore, if you’re interested in learning how to get the most out of this extension and want to start using it when developing your own PHP programs, then this series of articles might be what you’re looking for.

Welcome to the sixth part of a series covering the use of filters in PHP 5. Comprised of nine tutorials, this series explores the most relevant filters that come with this handy library, and shows you with practical examples how to use them for checking strings and numeric values, email addresses and URLs, and many other common types of data.

And now that you’ve been introduced to the main subject of this series, it’s time to spend a few minutes reviewing the topics that were discussed in the last installment. In that particular tutorial I explained how to work with regular expressions by using the FILTER_VALIDATE_REGEXP filter in conjunction with the already familiar “filter_var()” function. Also, I showed how to validate well-formatted URLs at a very basic level by means of another helpful filter, not surprisingly called FILTER_VALIDATE_URL.

As I said, the validation process performed on URLs was pretty simplistic. Thus, in this sixth episode of the series I’m going to dig deeper into the usage of the FILTER_VALIDATE_URL filter to show you how to validate different portions of a URL, including its protocol, host and eventual paths.

So, are you ready to learn how to accomplish all of these useful things with the filter PHP 5 extension? Then let’s begin now!

{mospagebreak title=Review: the FILTER_VALIDATE_URL filter}

If you haven’t had the chance to read the previous chapter of the series, where I coded a few basic examples showing how to validate URLs using the FILTER_VALIDATE_URL filter, you’ll find those examples listed below. Here they are:

(example on validating a well-formatted URL with the FILTER_VALIDATE_URL filter)

 

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

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

{

echo ‘The URL provided is not valid.';

}

else

{

echo ‘The URL provided is valid.';

}

 

 

 

(example on validating a badly-formatted URL with the FILTER_VALIDATE_URL filter)

 

$url = ‘http://';

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

{

echo ‘The URL provided is not valid.';

}

else

{

echo ‘The URL provided is valid.';

}

As you may recall, performing basic validation on URLs with the FILTER_VALIDATE_URL filter is a very simple process that should be easy for you to grasp. In the code samples listed above, the filter is used in its simplest version, meaning that it’ll only check that the URLs being validated are well-formatted and nothing else.

As I mentioned in the introduction, however, it’s possible to instruct the filter to analyze different portions of a URL, including its protocol, host and paths, thus performing a more strict validation process. So, assuming that you’re interested in learning how to accomplish these helpful tasks, in the section to come I’m going to discuss how to utilize the FILTER_VALIDATE_URL filter in a more useful manner.

As usual, to learn more on this specific topic, please click on the link that appears below and keep reading.

{mospagebreak title=Validating URLs more strictly with the FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED constants}

As I anticipated in the section that you just read, the filter extension allows you to validate URLs more strictly, by using a few additional arguments in conjunction with the FILTER_VALIDATE_URL filter. Of course, to illustrate more clearly how these arguments can be used in concrete cases, below I coded two brand new examples. They make use of the FILTER_FLAG_SCHEME_REQUIRED option for validating the correct scheme of a given URL. Look at them, please:

(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.';

}

Hopefully, the examples shown above give you a clear idea of how to validate the schema of a URL by using the FILTER_FLAG_SCHEME_REQUIRED option, which has been passed as an additional argument to the “filter_var()” function. In the first case, the schema of the URL being checked is actually correct, so the script will display a successful message on screen. On the other hand, the last example will complain about the URL’s schema, since it’s simply incorrect. 

Now that you’ve learned how to validate the schema of a specified URL, let me show you another pair of examples which demonstrate how to determine if the URL in question contains a host portion. This time we’ll use an entirely new  argument called FILTER_FLAG_HOST_REQUIRED. Here are the aforementioned examples:

 

// 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.';

}

Do you realize how simple it is to determine if a specified URL includes a host section? I guess you do. However, if you have any doubts about how this process is achieved, the above examples should help to dissipate them quickly. As you can see, in both cases the FILTER_FLAG_HOST_REQUIRED argument is passed to the “filter_var()” function to check if the host portion of the URL being validated is present or not.

Of course, the first example will consider the incoming URL to be valid. The second case, on the other hand, will display a trivial error message on the browser, since the host hasn’t been specified.

So far, so good, right? At this stage, I’m sure that you’ve already grasped the logic that stands behind validating specific sections of a URL by means of the FILTER_VALIDATE_URL filter and the “filter_var()” function. Therefore, the last thing that I’m going to discuss in this tutorial will consist of demonstrating how to use the filter to determine if a given URL contains a path portion. As you might have guessed, this task will be performed through another additional argument of the FILTER_VALIDATE_URL filter. But if you wish to learn the full details of this process, now click on the link below and read the following segment.

{mospagebreak title=Validating paths within URLs: the FILTER_FLAG_PATH_REQUIRED argument}

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!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan