Blogger Information
Blog 19
fans 0
comment 1
visits 19329
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP判断URL的合法性(是否为URL链接)
TANKING的代码日志
Original
1529 people have browsed it

在开发中,为了有效过滤恶意内容或者错误操作导致的错误参数造成安全问题,我们应该做进一步的验证措施,保证数据的准确,合法,安全。所以这里介绍了URL的合法性验证,用于验证用户输入的URL是不是正确的URL,可用的URL,而不是其他参数都能被当成URL传过去。

1、正则表达式

  1. function is_url($url){
  2. $r = "/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is";
  3. if(preg_match($r,$url)){
  4. //return true;
  5. echo '正确的 url 地址';
  6. }else{
  7. //return false;
  8. echo '不是合法的 url 地址';
  9. }
  10. }

2、内置函数filter_var()

  1. function is_url_2($url){
  2. if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
  3. echo 'url 地址正确';
  4. }else{
  5. echo 'url 地址不正确';
  6. }
  7. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!