lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
PHP: Is Machine Online?
If you're running PHP on a Windows like I am, the following code will quickly tell you whether you are online:

// is machine online? (for XP)
// returns ip if machine is online, false if offline (per ipconf)
/*____________________________________________________________________________*/
function is_machine_online()
{
$ip = false; // return

$re_ip = '%(([0-9]+\.){3}([0-9]+))%'; // ip regex

// WIN only (add ifconfig in future for Unix)
if ( substr( PHP_OS, 0, 3 ) != 'WIN' )
{
trigger_error("this fx currently only works with WIN", E_USER_WARNING);
return false;
}

exec('ipconfig', $OUT, $failed);
if ( $failed ) return false;

foreach ( $OUT as $ln )
{
if ( stripos($ln, 'ip address') === FALSE ) continue;
if ( preg_match($re_ip, $ln, $MATCH) )
{
$ip = $MATCH[0];
break;
}
}

return $ip;
}
/*____________________________________________________________________________*/


Usage:
if ( $test = 1 )
{
$debug = 0;
print 'testing is_machine_online()';
print '<pre>';

// test code here
print $is = ( is_machine_online() ) ? 'is online' : 'not online';

print '</pre>';
}

Labels: