[PHP] Is URL online?
Return true if URL is online.
Copy this code and paste it in your HTML
function is_online( $url ){
if (!$url) return false;
$url['port'] = (!isset($url['port'])) ?
80 : (int
)$url['port'];
$path = (isset($url['path'])) ?
$url['path'] : '/';
$path .= (isset($url['query'])) ?
"?$url[query]" : '';
$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
if (!$fp) return false; //socket not opened
fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
//socket opened $headers = fread($fp, 4096);
if(preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers)){ //matching header
return true;
}
else return false;
} // if parse url
else return false;
}
Report this snippet