Created
August 20, 2012 16:01
-
-
Save cam-gists/3405374 to your computer and use it in GitHub Desktop.
PHP: Strict URL Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.