【问题标题】:How to PHP server-side check if a url of a web site is valid?如何 PHP 服务器端检查网站的 url 是否有效?
【发布时间】:2011-04-23 11:46:26
【问题描述】:

我搜索了我们的 SO 问题,但发现解决方案基于调用 ping 使用 system PHP 函数执行的命令。

我的虚拟主机服务器不允许我这样做。我该怎么办?

请帮忙, 南。

更新

我需要通过server side查看。

【问题讨论】:

  • 更多上下文将有助于找到一个好的解决方案。您对此的确切用例是什么?您需要验证 URL 的目的是什么? URL 是否需要可访问,还是仅有效?

标签: php validation url server-side


【解决方案1】:

如果有效 URL 是指不是 404 的 URL,则可以使用 get_headers() 并在第一个返回的数组元素中查找 404

$url = 'http://google.com';
list($status) = get_headers($url);
if (strpos($status, '404') !== FALSE) {
   // URL is 404ing
}

或者,您可以查找200,这将是一个正常的快乐请求:)

【讨论】:

    【解决方案2】:

    试试这个:

    //checking if the site exists by fopen, instead of file_get_contents to speed it up
    
    $url = "URL"; //your url goes in this place instead of nabtron.com
    
    if (@fopen($url,"r")) {
    echo "<b>".$url."</b> is accessible<br />";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多