Skip to content

Instantly share code, notes, and snippets.

Created January 21, 2015 17:19
What would you like to do?
PHP ping script
* Use the examples below to add your own servers. Coded by clone1018 [?]
$title = "Simple Server Status"; // website's title
$servers = array(
'Google Web Search' => array(
'ip' => '',
'port' => 80,
'info' => 'Hosted by The Cloud',
'purpose' => 'Web Search'
'Example Down Host' => array(
'ip' => '',
'port' => 8091,
'info' => 'ShittyWebHost3',
'purpose' => 'No purpose'
if (isset($_GET['host'])) {
$host = $_GET['host'];
if (isset($servers[$host])) {
header('Content-Type: application/json');
$return = array(
'status' => test($servers[$host])
echo json_encode($return);
} else {
header("HTTP/1.1 404 Not Found");
$names = array();
foreach ($servers as $name => $info) {
$names[$name] = md5($name);
<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" href="//">
<link rel="stylesheet" href="//">
<style type="text/css">
/* Custom Styles */
<div class="container">
<h1><?php echo $title; ?></h1>
<table class="table">
<?php foreach ($servers as $name => $server): ?>
<tr id="<?php echo md5($name); ?>">
<td><i class="icon-spinner icon-spin icon-large"></i></td>
<td class="name"><?php echo $name; ?></td>
<td><?php echo $server['info']; ?></td>
<td><?php echo $server['purpose']; ?></td>
<?php endforeach; ?>
<script src="//"></script>
<script type="text/javascript">
function test(host, hash) {
// Fork it
var request;
// fire off the request to /form.php
request = $.ajax({
url: "<?php echo basename(__FILE__); ?>",
type: "get",
data: {
host: host
beforeSend: function () {
$('#' + hash).children().children().css({'visibility': 'visible'});
// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR) {
var status = response.status;
var statusClass;
if (status) {
statusClass = 'success';
} else {
statusClass = 'error';
$('#' + hash).removeClass('success error').addClass(statusClass);
// callback handler that will be called on failure (jqXHR, textStatus, errorThrown) {
// log the error to the console
"The following error occured: " +
textStatus, errorThrown
request.always(function () {
$('#' + hash).children().children().css({'visibility': 'hidden'});
$(document).ready(function () {
var servers = <?php echo json_encode($names); ?>;
var server, hash;
for (var key in servers) {
server = key;
hash = servers[key];
test(server, hash);
(function loop(server, hash) {
setTimeout(function () {
test(server, hash);
loop(server, hash);
}, 6000);
})(server, hash);
/* Misc at the bottom */
function test($server) {
$socket = @fsockopen($server['ip'], $server['port'], $errorNo, $errorStr, 3);
if ($errorNo == 0) {
return true;
} else {
return false;
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
return false;
Copy link


Copy link

Thx used this to generate a live report of our printer fleet

Copy link

pavs commented Aug 21, 2015

I am a noob programmer myself, I could use some advice on how I could re-purpose this code to do ping without specifying any ports. Because the ips I want to see if they are live or not don't have any services running. Only way to see if they are up is to ping or traceroute.

Copy link

Hey man, i got this Script working on my website, but i also added in LINKS but when ever it "refreshes" the Links that i provided in there VANISH. i don't know why or can't seem to figure it out. but could you possibly assist me on this?

Copy link

Attempting to convert this from it's current "test" function of fsockopen to ping, so that I can test servers with no services running - anyone able/willing to help me out?

Copy link

DBJDBJ commented Mar 16, 2016

This is not a ping implementation. This is server-is-alive...

Copy link

how to combine with MYSQL database

Copy link

hbosko commented Oct 24, 2016

Thanks for avesome script.
Its posible to load arrays with separate XML file?

Copy link

Tuliol commented Mar 11, 2017

Hi, How to combine with a mysql database, thank you very much

Copy link

thx :)

Copy link

Good script.

Copy link

help me? pls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment