Forum Moderators: coopster

Message Too Old, No Replies

Checking if a url exists

with php

         

Richie0x

8:07 pm on May 6, 2004 (gmt 0)

10+ Year Member



Is there a way using php to check if a url exists?

One of my scripts grabs the html of another page, the url of which is not set. I need to check if the page actually exists before connecting to it to get the html.

coopster

9:09 pm on May 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



PHP has a couple of functions to test for file existence, file_exists [php.net] and is_file [php.net]. As of PHP 5.0.0 these functions can also be used with some URL wrappers.

However, prior to PHP 5.0.0 these functions will not work on remote files as the file to be examined must be accessible via the servers filesystem. Why not just try to open the file and monitor for errors? You could simply suppress any errors by using the PHP error control operator (@) [php.net]. Something along the lines of ...

if (@file_get_contents('http://mysite.com')) { 
// success!
} else {
// failure :(
}

Netizen

3:42 pm on May 7, 2004 (gmt 0)

10+ Year Member



You could also take a look at fsockopen [php.net] as you could separate out domain and URL problems.