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

…衆ロ難τιáo~ 提交于 2019-11-27 08:03:22

问题


I've searched our SO questions but found solutions are based on calling ping command executed from using system PHP function.

My web host server doesn't allow me to do that. What should I do?

Please help, Nam.

Update

I need to check from the server side.


回答1:


If by a valid URL you mean one which does not 404, then you could use get_headers() and look for 404 in the first returned array element.

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

Alternatively, you could look for 200 which would be a normal happy request :)




回答2:


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


来源:https://stackoverflow.com/questions/5764002/how-to-php-server-side-check-if-a-url-of-a-web-site-is-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!