如何PHP服务器端检查一个网站的url是否有效


How to PHP server-side check if a url of a web site is valid?

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

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

请帮忙,南。

更新

我需要从server side检查。

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

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

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

try this:

//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 />";
}