PHP Validate URL

Posted on January 10, 2017 at 11:21 pm

PHP function that validates and URL:

function is_website_url($url, $allowed_schemes = array('http', 'https')) {
    $validate_url = !(filter_var($url, FILTER_VALIDATE_URL) === false);
    $scheme = parse_url($url, PHP_URL_SCHEME);
    return $validate_url && in_array($scheme, $allowed_schemes, true);
}

Usage:

$url = $_GET['url'];
if (!is_website_url($url)) {
    die('Unsafe url detected.');
}

Found on SECURITY.md of PHP Curl Class.

Stay Updated

Other Posts

Updated Posts