Created
September 22, 2015 15:00
`filter_input` validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$recaptcha = filter_input(INPUT_POST, 'g-recaptcha-response', FILTER_CALLBACK, ['options' => validateReCaptcha('<your-secret-here>')]); | |
$phone = filter_input(INPUT_POST, 'phone', FILTER_CALLBACK, ['options' => validateTelephoneNumber()]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function validateReCaptcha($secret = '') | |
{ | |
return function ($field) use ($secret) { | |
$recaptcha = new \ReCaptcha\ReCaptcha($secret); | |
return $recaptcha->verify($field, $_SERVER['REMOTE_ADDR'])->isSuccess(); | |
}; | |
} | |
function validateTelephoneNumber($strict = false, $strictRegex = '') | |
{ | |
return function ($string) use ($strict, $strictRegex) { | |
$regex = $strictRegex ?: "/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i"; | |
if (!$strict) { | |
return strlen(preg_replace("/[^0-9]/", "", $string)) >= 7; | |
} | |
return preg_match($regex, $string); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment