PHP Validate URL

Posted on January 10, 2017 at 11:21 pm

Quick PHP function to validate an URL:

if(filter_var($url, FILTER_VALIDATE_URL)) {
    echo "URL is a valid URL";
} else {
    echo "URL is not a valid URL";


Best PHP function that validates an URL checking schemes for safety:

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);


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

Found on of PHP Curl Class.

Updated on April 29, 2017 at 1:32 pm

Other Posts

Updated Posts