douzhaocheng4533 2015-01-18 07:36
浏览 35
已采纳

验证URL - URL在没有TLD的情况下有效吗?

So, I have an input box where someone can input a URL. When they submit the forum, I would like it to check if it is a valid URL.

Now, my problem is that "http://someurl" (without the TLD) is valid, and "someurl.com" or "www.someurl.com" is not valid (without the http://). Is there a way to fix this?

$input = 'someurl.com'; // Could also be an IP

if (filter_var($input, FILTER_VALIDATE_IP)) {
    echo "IP Valid";
} else if (filter_var($input, FILTER_VALIDATE_URL)) {
    echo "URL Valid";
} else {
    echo "Not an IP or URL";
}
  • 写回答

1条回答 默认 最新

  • doulaozhi6835 2015-01-18 07:44
    关注

    The fix is to insert http:// yourself as an URL must have a scheme. This is why the simple www.someurl.com is invalid: it doesn't have a scheme.

    URL without top-level domains are perfectly fine, like http://localhost. In a LAN with correctly set search domain you also can use URLs without a domain: for example, if the search domain of your company is somecompany.com and you have the URL http://fileserver, your system will internally build the fully qualified domain name fileserver.somecompany.com. Some URLs don't have hosts at all (for example, tel:), BTW.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?