PHP Snippets
site_is_up
Information
(PHP Version >= 3)Functions used in this snippet: fsockopen, str_replace, fclose.
Description
bool site_is_up( str site, int port0, int timeout )
checks site on port port to see if the site is accepting connections (whether the site is up or not). If site is down it will take [i]timeout[i] seconds to timeout. Returns true on success and false on failure.
Snippet
<?php
function site_is_up($site,$port=80,$timeout=1){
$port = $port === NULL ? 80 : $port;
$fp = @fsockopen(str_replace('http://','',$site), $port, $errno, $errstr, $timeout);
if($fp === false){
return false;
}
fclose($fp);
return true;
}
?>
Example
site_is_up Can be used in the following way:
<?php
if(site_is_up('www.bigtoach.com')){
echo 'Site is up!!';
}else{
echo 'Site is down.';
}
// self explanitory I hope
if(site_is_up('bigtoach.com',NULL,5)){ echo 'UP!'; }
// check bigtoach.com with a 5 second timeout (still on port 80)
if(site_is_up('bigtoach.com',2082)){ echo 'UP!'; }
// check bigtoach.com on port 2082
?>
Updated on Jun 27th at 6 pm.
0 Responses to site_is_up