Speed up development with full-stack environments for every branch.

Learn More

Check if a input variable exists with filter_has_var() function

62 Runs 937 Views 160 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * PHP filter module
 * checks if a input variable exists using filter_has_var() function
 */
//creating simple POST form
$formPOST = <<<FORM
<form method="POST">
Enter <b>email</b> for POST validation: <input type="text" name="email" value="{$_POST['email']}">
<input type="submit" name="check" value="check">
</form>
FORM;

//creating simple GET form
$formGET = <<<FORM
<form method="GET">
Enter <b>url</b> for GET validation: <input type="text" name="url" value="{$_GET['url']}">
<input type="submit" name="check" value="check">
</form>
FORM;

echo '<center>';
echo $formPOST;
echo $formGET;

//POST check
if (isset($_POST['check'])) {
    //check if the variable 'email' of type INPUT_POST exists
    $checkPOST = filter_has_var(INPUT_POST, 'email');
    echo '<div style="background-color:yellow;padding:10px;color:#000;font-size:16px;">POST METHOD</div>';
    if ($checkPOST) {
        echo '<div style="background-color:green;padding:10px;color:#fff;font-size:16px;">
            <b>$_POST[\'email\']</b> exists
          </div>';
    } else {
        echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px;">
            <b>$_POST[\'email\']</b> does not exists
          </div>';
    }
}
//GET check
elseif (isset($_GET['check'])) {
    //check if the variable 'url' of type INPUT_GET exists
    $checkGET = filter_has_var(INPUT_GET, 'url');
    echo '<div style="background-color:yellow;padding:10px;color:#000;font-size:16px;">GET METHOD</div>';
    if ($checkGET) {
        echo '<div style="background-color:green;padding:10px;color:#fff;font-size:16px;">
           <b>$_GET[\'url\']</b> exists
          </div>';
    } else {
        echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px;">
           <b>$_GET[\'url\']</b> does not exists
          </div>';
    }
}
echo '</center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).