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

Validating URLs more strictly with the FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED constants - 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

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.



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