Simple Examples

From PhpInputValidator

Jump to: navigation, search

Contents

Preface

These are simple examples to show how to use phpInputValidator. In all these examples it assumes:

  1. That you have installed phpInputValidator and have it configured.
  2. The the variable $getvar is assigned to an instance phpInputValidator class

Example 1: Basic getvar

Lets say for example we have the following URL http://www.yoursite.com/index.php?page_id=1 and you need to fetch the page_id which is supposed to be an integer. In PHP you could use:

$page_id = $GET['page_id'];

I would hope you have enough of an understanding of PHP enough to know that is not a safe way to get that. So instead you could use:

$page_id = (int)$GET['page_id'];

--OR--

$page_id = intval($GET['page_id']);

Well lets see how you could do that with phpInputValidator:

$page_id = $getvar->int(array('variable' => 'page_id', 'location' => 'GET'));

In this example we are using the variable getvar which is assigned an instance of the phpInputValidator class. We are using the method int() which is a shortcut method to the get function with a type of int. Basically means we want an integer.
In the first element of the array we are passing a key of variable with data page_id. This is the variable name you want to fetch. So for this example it's page_id.
In the second element of the array we are passing has a key of location with the data of GET. This is location of the variable. This can only be GET, POST, REQUEST, COOKIE, SESSION, or SERVER. This value is always a string.

Example 2: More advanced getvar

Lets say for this example you have a web form that is used to email the following information to you:

   * First name
         o Alpha only
         o input name first_name
   * Last name
         o Alpha only
         o input name last_name
   * Phone number
         o US in this example
         o input name phone
   * Email address
         o input name email
   * Message
         o You allow HTML
         o input name message

All this information is sent using POST. Lets also say in this example that First Name and Last Name are optional. If they are not there we want the field to say N/A. The message we will allow HTML.

So lets start getting that data.

$first_name = $getvar->alpha(array('variable' => 'first_name', 'location' => 'POST', 'max_length' => 255, 'default' = 'N/A'));

Lets break this down a bit. So you should have an understanding of the first 3 array elements from the previous example. In this case instead of an integer we are looking for alpha method. Alpha means only alpha characters (a-z). You might notice a couple of other elements. In this case we want the maximum length of the string to be 255 characters. To do this we use pass in the element of max_length with a value of 255. The element default is the default value to pass back if at any point the variable doesn't validate. In this case we want it to be the string 'N/A'.

$last_name = $getvar->alpha(array('variable' => 'last_name', 'location' => 'POST', 'max_length' => 255, 'default' = 'N/A'));

This will get the last name. Since this is pretty much the same as the first name, you should understand everything in this example.

$phone = $getvar->us_phone(array('variable' => 'phone', 'location' => 'POST'));

In this example we are getting the type of us_phone. What this means it will validate the input for a United Stated phone number (ie (555)555-5555 or 555-555-5555).

$email = $getvar->email(array('variable' => 'email', 'location' => 'POST'));

This should now be pretty easy to figure out. We are fetching the email address.

$message = $getvar->html(array('variable' => 'message', 'POST', 'location' => 'POST'));

Note

The important thing in this example is we are using the html method. This is an option plugin that requires HTMLPurifier to be installed and configured. Without it you cannot use html, you could use string however this will string any HTML code.

Example 3: Multiples of the same

Lets say that you are going to fetch multiple of the same items. For example lets say you have a form that asks for phone_number, cell_number, and fax_number and all those are going to be us_phone numbers.

You could do:

    $phone_number = $getvar->us_phone(array('variable' =>'phone_number', 'location' => 'POST'));
    $cell_number = $getvar->us_phone(array('variable' =>'cell_number', 'location' => 'POST'));
    $fax_number = $getvar->us_phone(array('variable' =>'fax_number', 'location' => 'POST'));

But an easier way would be:

list($phone_number, $cell_number, $fax_number) = $getvar->us_phone(array('variable' => array('phone_number', 'cell_number', 'fax_number'), 'location' => 'POST'));

By default the array that is return is not indexed. However if you pass in 'key' => true it will return an array keyed by the variable name.

Conclusion

These are very simplistic examples of how to use the library. You can do much more than this. Please spend some time to look through the wiki. If you have any questions, comments, etc. please feel free to put in a support ticket or leave me a message here. I love feedback.