Skip to content

Instantly share code, notes, and snippets.

@QWp6t
Created September 22, 2015 15:00
Show Gist options
  • Save QWp6t/334d2c26f1bd139ccf80 to your computer and use it in GitHub Desktop.
Save QWp6t/334d2c26f1bd139ccf80 to your computer and use it in GitHub Desktop.
`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