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

前端 未结 2 939
轻奢々
轻奢々 2020-12-11 20:26

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

My web host

相关标签:
2条回答
  • 2020-12-11 20:52

    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 :)

    0 讨论(0)
  • 2020-12-11 20:53

    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 />";
    }
    
    0 讨论(0)
提交回复
热议问题