Phpinputvalidator.org Example

From PhpInputValidator

Jump to: navigation, search

This example shows how the old index.php (now download.php) on phpinputvalidator.org responds to your request to download a file (the links on the right side for .zip or .tar).

When you click on the link on the index.php on phpinputvalidator.org it sends you to:

index.php?download=zip

or

index.php?download=tar

Depending on which link you clicked on. The index.php file has to figure out which file to send to you. It does that with:

        if (isset($_GET['download'])) {
                require dirname(__FILE__).'/lib/phpInputValidator.php';
                $getvar = phpInputValidator::getInstance();
                $download = $getvar->string(array('variable' => 'download', 'location' => 'GET', 'valid_values' => array('zip', 'tar')));
                if (!empty($download)) {
                        $filename = 'phpInputValidator.';
                        if ($download == 'tar') {
                                $filename .= 'tar.gz';
                        } else if($download == 'zip') {
                                $filename .= 'zip';
                        }
                }
        }

This is an actual snippet from the live file (it has been trimmed down). Let's break it down.

if (isset($_GET['download'])) {

There is no sense in using phpInputValidator if it is not needed. Since this is the only place in the file it is needed and it's only when download is equal to something, we can just check for the $_GET['download'].

require dirname(__FILE__).'/lib/phpInputValidator.php';

This loads the library using the direct path.

$getvar = new phpInputValidator();

This assigns $getvar to a new instance of phpInputValidator.

$download = $getvar->string(array('variable' => 'download', 'location' => 'GET', 'valid_values' => array('zip', 'tar')));

$download will not be equal to the result of the get method of phpInputValidator.
We are looking for download in the GET. It should be a string type.
It must be equal to:

'zip'

OR

'tar'

Obviously because these are the only two type of that file and those are my only two links.

if (!empty($download)) {

If the download is valid, thus not equal to null.

From here we set the file name to correct file type. Then (not shown) we send the user the file.