How To Ping a Website or Server With PHP

by Oscar

How to Ping a website or server?

We all know to ping a website in the case of windows OS (XP, W7, W8) you simply open your command window (Start->Run, type in ‘CMD’ and press Enter), Type in ‘Ping www.google.com’.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

Replace ‘www.google.com’ with the IP or website domain name with the one you want, and you should get the result.

With this PHP script, you can ping a website, server or terminal(e.g. PC) that is within your network, which means you don’t need to sit in front of a computer to check if a server or website is down, but also through your mobile phone, tablet etc that has internet access.

It’s simply a single function, with some modification you can make it really powerful and useful.

[sourcecode language=”php”]
<?php

##############################################
#
# Script By Oscar Liang – www.oscarliang.com
# Version 1.0
# 01/March/2013
#
##############################################

function ping($target){

$result = array();

/* Execute Shell Command To Ping Target */
$cmd_result = shell_exec(“ping -c 1 -w 1 “. $target);

/* Get Results From Ping */
$result = explode(“,”,$cmd_result);

/* Return Server Status */
if(eregi(“0 received”, $result[1])){
return ‘offline’;
}
elseif(eregi(“1 received”, $result[1])){
return ‘online’;
}
else{
return ‘unknown’;
}

}

$target = ‘google.com’;

echo ‘”‘.$target.'”‘ . ‘ is ‘ . ping ( $target );

?>
[/sourcecode]

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.

6 comments

sanjay makwana 4th March 2016 - 7:33 pm

i am run on windows os this code error is =>

Parse error: syntax error, unexpected ‘1’ (T_LNUMBER) in F:\XAMPP\htdocs\code\ping gl.php on line 16

Reply
balwinder 22nd February 2016 - 12:01 pm

This is working man!!… Thank you

Reply
Conrad Bate 31st December 2013 - 11:35 am

NB:: I couldn’t get SELINUX to stop stopping the shell_exec (after changing the following variables httpd_tmp_exec, allow_httpd_anon_write, httpd_can_network_relay, httpd_can_network_connect, httpd_ssi_exec
httpd_can_network_connect_cobbler, httpd_verify_dns, httpd_execmem) I turned if off by edtting `/etc/sysconfig/selinux` to say “disabled” and rebooting machine

Reply
Conrad Bate 30th December 2013 - 12:46 pm

on centos you have to run as root

# setsebool -P httpd_ssi_exec 1

on CentOS run this line to get this script to work : by default `shell_exec` will not run under SELinux

Reply
Daniel Price 17th August 2013 - 3:42 pm

Thanks for your help, but here’s a similar function that I made from parts of yours that returns an array of results:
[0] = min ping time,
[1] = avg ping time,
[2] = max ping time,
[3] = mdev of ping times,

You can specify the IP / Web address and the number of pings (hopefully useful).

$results = ping(ip_address, pings)
// min = $results[0];
// avg = $results[1];
// max = $results[2];
// mdev = $results[3];

The function:

function ping($t,$p){
$r = array();
$st = “ping -c “.$p.” -w “.$p.” “.$t;
$cr = shell_exec($st);
$r = explode(“,”,$cr);
$so = $r[3];
$st = ‘max/mdev =’;
$so = substr($so,strpos($so,$st)+strlen($st));
$st = ‘/’;
$min = substr($so,0,strpos($so,$st));
$so = substr($so,strpos($so,$st)+strlen($st));
$avg = substr($so,0,strpos($so,$st));
$so = substr($so,strpos($so,$st)+strlen($st));
$max = substr($so,0,strpos($so,$st));
$so = substr($so,strpos($so,$st)+strlen($st));
$st = ‘ms’;
$mdev = substr($so,0,strpos($so,$st));
$so = substr($so,strpos($so,$st)+strlen($st));
return array($min, $avg, $max, $mdev);
}

An example:

$websiteOrIP = “www.google.com”;
$results = ping($websiteOrIP,4);
echo(“Average ping time for “.$websiteOrIP.” was “.$results[1]);

– Dan.

Reply
Oscar 18th August 2013 - 12:16 pm

Thanks for the idea Dan!
I will test it when i have a chance.

Regards
Oscar

Reply