php code to wakeup a sleeping computer
name your file similar to this wakeupcomputername.php
<?php
flush();
function WakeOnLan($addr, $mac)
{
$addr_byte = explode(':', $mac);
$hw_addr = '';
for ($a=0; $a < 6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false)
{
echo "Error creating socket!<br>\n";
echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s)) . "<br>\n";
}
else
{
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if($opt_ret < 0)
{
echo "setsockopt() failed, error: " . strerror($opt_ret) . "<br>\n";
}
$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
socket_close($s);
echo "A wake up command was sent to computer with the mac address.<br>\n";
echo "Magic Packet sent (".$e.") to ".$addr.", MAC=".$mac."<br>\n";
echo "Wait some seconds before trying to access the computer.<br>\n";
}
}
#WakeOnLan('yourIPorDomain.dyndns.org', 'your:MAC:address');
#WakeOnLan('192.168.1.255', '00:01:1C:10:04:03');
//if you have switch or other routing devices in LAN, sending to
// the local IP doesn't help! you need send to the broadcast address like this:
WakeOnLan('192.168.1.255', '00:18:4d:f0:09:f9');
//system("ping 192.168.1.51",$return);
?>