regex - PHP url validation false positives -
for odd reason if statement check urls using filter_validate_url returning unexpected results.
simple stuff https://www.google.nl/ being blocked www.google.nl/ isn't? not blocks every single url http or https infront of either. allowed , others not, know there bunch of topics of them using regex filter urls. beter using filter_validate_url? or doing wrong?
the code use check urls this
if (!filter_var($linkinput, filter_validate_url) === false) { //error code }
you should filter first. (just measure).
$url = filter_var($url, filter_sanitize_url);
the filter_validate_url accepts ascii url's (ie, need encoded). if above function not work see php urlencode() encode url.
if that doesn't work, should manually strip http: beginning ...
$url = strpos($url, 'http://') === 0 ? substr($url, 7) : $url;
here flags might help. if all of url's have http://
can use filter_flag_scheme_required
the filter_validate_url filter validates url.
possible flags:
- filter_flag_scheme_required - url must rfc compliant (like http://example)
- filter_flag_host_required - url must include host name (like http://www.example.com)
- filter_flag_path_required - url must have path after domain name (like www.example.com/example1/)
- filter_flag_query_required - url must have query string (like "example.php?name=peter&age=37")
the default behavior of filter_validate_url
validates value url (according » http://www.faqs.org/rfcs/rfc2396), optionally required components.
beware valid url may not specify http protocol http:// further validation may required determine url uses expected protocol, e.g. ssh:// or mailto:.
note function find ascii urls valid; internationalized domain names (containing non-ascii characters) fail.
Comments
Post a Comment