Code:
<?php
if(isset($_GET['url'])) {
$url = $_GET['url'];
$port = (isset($_GET['port']) && $_GET['port'] != "") ? $_GET['port'] : 80;
if(!preg_match("@^https?://@",$url)) $url = "http://" . $url;
preg_match("@^https?://[^/]+@",$url,$parts);
$url = $parts[0];
$isup = checkit($url, $port);
if($isup == false) {
echo $isup;
die("<a href='$url' target='_new' rel='nofollow'>$url:$port</a> <font color='red'>is down!</font>");
} else
die("<a href='$url' target='_new' rel='nofollow'>$url:$port</a> <font color='green'>is up!</font>");
}
function checkit($url, $port) {
$url = @parse_url($url);
$port = intval($port);
if (!$url) return false;
$url = array_map('trim', $url);
$path = (isset($url['path'])) ? $url['path'] : '/';
$path .= (isset($url['query'])) ? "?$url[query]" : '';
if (isset($url['host']) && $url['host'] != gethostbyname($url['host'])) {
$fp = @fsockopen($url['host'], $port, $errno, $errstr, 30);
if (!$fp) return false;
fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
$headers = fread($fp, 4096);
fclose($fp);
if(preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers) || ($port != 80 && $headers != "")) {
return true;
} else
return false;
}
else
return false;
}
?>
<script type="text/javascript">
function checkServerStatus(url, port) {
var getURL = $('#domain').val();
var getPort = $('#port').val();
$('#result').html('<font color="blue">Checking...</font>');
$.ajax({
url: '/isup.php',
type: 'GET',
data: {
url: getURL,
port: getPort
},
cache: false,
success: function (data) {
console.log(data);
$('#result').html(data);
},
error: function () {
$('#result').html('<font color="red">Check failed, please try again!</font>');
}
})
return false;
}
</script>
<style type="text/css">
#isup{text-align:center;height:120px;width:800px;}
#isup a {font-size:20px;margin:auto;font-weight: bold;margin-top:10px;}
#isup #result{font-size:20px;margin:auto;font-weight: bold;margin-top:10px;}
#isup #check{vertical-align: bottom;height: 38px;}
#isup #domain {
border: 1px solid rgb(148, 141, 141);
background: #ffc;
display: inline-block;
padding: 8px;
height:20px;
width: 200px;
font-size: 1.0em;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
-webkit-box-shadow: 5px 5px 3px rgba(0,0,0, .75);
-moz-box-shadow: 5px 5px 3px rgba(0,0,0, .75);
box-shadow: 5px 5px 3px rgba(0,0,0, .75);
}
#isup #port {
border: 1px solid rgb(148, 141, 141);
background: #ffc;
display: inline-block;
padding: 8px;
height:20px;
width: 50px;
font-size: 1.0em;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
-webkit-box-shadow: 5px 5px 3px rgba(0,0,0, .75);
-moz-box-shadow: 5px 5px 3px rgba(0,0,0, .75);
box-shadow: 5px 5px 3px rgba(0,0,0, .75);
}
#isup input[type=text]{border:2px dotted #ccc;color:#000;font-size:20px;padding:4px;width:400px;height:42px;}
#isup input[type=text]:hover {
border: 1px solid rgb(148, 141, 141);;
background: #ff6;
}
#isup input[type=submit]{
cursor: pointer;
padding: 5px 40px;
background: #35b128;
border: 1px solid #33842a;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
-webkit-box-shadow: 5px 5px 3px rgba(0,0,0, .75);
-moz-box-shadow: 5px 5px 3px rgba(0,0,0, .75);
box-shadow: 5px 5px 3px rgba(0,0,0, .75);
color: #f3f3f3;
font-size: 1.3em;
display: inline-block;
}
#isup input[type=submit]:hover{
background-color :#399630;
-webkit-box-shadow: 5px 5px 3px rgba(0,0,0, .75);
-moz-box-shadow: 5px 5px 3px rgba(0,0,0, .75);
box-shadow: 5px 5px 3px rgba(0,0,0, .75);
}
</style>
<div id="isup">
<form action="javascript: void(0);" onSubmit="checkServerStatus();">
<input type="text" size="25" id="domain" placeholder="Enter URL" />
<input type="text" id="port" placeholder="80" />
<input type="submit" id="check" value="Check" />
</form>
<div id="result"></div>
</div>