Ping Pong PHP Pinger


Include library

require 'phppinger.class.php';

Create a new instance

$ping = new PHP_Pinger();

Add host to be checked

The easy way:


You can also specified a port (default port is 80):

$ping->addHost('', 80);

You can optionnaly add a sweety name (default label is the hostname):

$ping->addHost('', 80, 'My blog');

PHP Pinger use a socket to check if the host is online. It opens a socket at the given host and port, and if no error occures the conclusion is that the host or service is up.

Run the ping


Nothing display? Don't be worry: by default if no callback function is given, PHP Pinger will ping hosts but display nothing.
To display the result, see next paragraph.

The run() method must be called at the end, otherwise other options will not be set.

Register a callback function

The callback function is called when a host has been pinged, that means the function is called for each host you added.

The callback function takes four parameters:

function myCallbackFunction($online, $host, $port, $label) {
    // the callback you want, for example:
    echo '<li>', $label, ' is ', ($online ? 'online' : 'offline'),
        ' (', $host, ':', $port, ')', '</li>';

Once definied, you have to add it to the created instance by:


Register pre/post run callbacks

Such as you registred the previous callback function, you can register a callback function executed before the run, and/or a callback function executed after the run:

// register an action done before run:

// register an action done after run:

You can combine all callbacks:



If your web server can send e-mail, you can be notify by e-mail. Notifications are by default disable.

Notifications levels

Three levels of notifications are available:

Set destinators to notify

To enable notifications, you have to add at least one destinator for e-mail:


Notifications could be sent to several e-mails:


Enable notifications

Just add the following line:


By default, enabling notifications will set the level to NotificationLevel::PROBLEMS.
You can specify another level, just add it as a parameter:

// enable PROBLEMS level (similar to previous line)

// enable REPORT level

Disable notifications

You have two ways to do that:

// dedicated method:

// or use the level NONE:

Personalize the subject's preffix

When an e-mail is sent, the default preffix is Status. You can change it with:

$ping->setNotificationPreffix('Network Monitoring');

Add an e-mail signature

To add a text at the end of the e-mail sent, just add:

$ping->setNotificationSignature('--'.PHP_EOL.'By PHP Pinger');


// load PHP Pinger
require 'phppinger.class.php';

/* callbacks for example */
function callback($is_online, $hostname, $port, $sweety_name) {
    echo '<li>', $hostname, ':', $port, ' is ',
    ($is_online ? 'online' : 'offline'), '</li>';
function preRun() { echo '<ul>', PHP_EOL; }
function postRun() { echo '</ul>', PHP_EOL; }

/* instanciate a new Pinger and run it with callbacks and notifications */
$ping = new PHP_Pinger();
    ->addHost('', 80)
    ->addHost('', 21, 'GitHub FTP')
    ->setNotificationTo(array('email@domain.tld', 'other-dude@domain.tld'))
    ->setNotificationPreffix('Network Monitoring')
    ->setNotificationSignature('--'.PHP_EOL.'By PHP Pinger')

You can combine methods as you want, except the run() method which must be at the end.