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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度