Skip to content

Instantly share code, notes, and snippets.

@cam-gists
Created August 20, 2012 16:01
Show Gist options
  • Select an option

  • Save cam-gists/3405374 to your computer and use it in GitHub Desktop.

Select an option

Save cam-gists/3405374 to your computer and use it in GitHub Desktop.
PHP: Strict URL Validation
<?php
$url = 'http://example.com';
$validation = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);
if ( $validation )
{
$output = 'proper URL';
}
else
{
$output = 'wrong URL';
}
echo $output;
@courtney-miles

Copy link
Copy Markdown

For anyone searching for strict URL validation, this is not how. PHPs URL validator is extremely accommodating and if you are paranoid about a URL being used for XSS, this is not the method to use.

For example, it will allow http://www.example.com/foo?<script>alert(1);</script> even though the specification says that < and > must be encoded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment