Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`filter_input` validation
<?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()]);
<?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
You can’t perform that action at this time.