Determining if a PC is online
How do I determine if a PC is online with PHP?
There are two ways to check if a PC is online. The first one uses the exec() function and the "ping" command to check if the PC is online. This does require that the PC has pinging enabled (i.e. it doesn't get blocked by the firewall of the PC). The code for this looks like this:
$str=exec("ping 127.0.0.1",$output,$a1);
if (count($output) > 1){
echo 'PC is online';
} else {
echo 'PC is NOT ONLINE!';
}
?>
If you don't want to use exec or ping, then you can also use the fsockopen function, and check any random port. The below code snippet uses port 135, but you can change this to almost any other port (as long as the port is unblocked by the PC).
$isThere = @fsockopen("127.0.0.1", 135, $errno, $errstr, 10);
if (!$isThere) {
echo 'PC is NOT ONLINE!';
} else {
echo 'PC is online';
}
?>
With either of the above code snippets you can now check if a PC is online and responding. A great way of using these code snippets is in a website monitoring / status script.
(Thanks to Leigh from PHPit Forums for the above tip!)