magicPacket('60:A4:4C:5F:C3:DA');
function magicPacket($mac,$addr='255.255.255.255',$socket_number=7) {
//split up the mac address based upon the colons in the string
$addr_byte = explode(':', $mac);
$hw_addr = '';
for ($a=0; $a <6; $a++)
$hw_addr .=chr(hexdec($addr_byte[$a])); //convert the hex to its decimal equivalent, encode as a character, and repeat 16 times
$msg = str_repeat(chr(255),6); //FF in decimal is 255, which is then encoded as a char as with our mac address
for ($a = 1; $a <= 16; $a++)
$msg .= $hw_addr;
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); //create our socket
if ($s == false) {
echo "Error creating socket!\n";
echo "Error code is '".socket_last_error($s)."'- " . socket_strerror(socket_last_error($s));
return false;
}
else {
// setting a broadcast option to socket:
//$opt_ret = socket_set_option($s, 1, 6, TRUE);
$opt_ret = socket_set_option($s,SOL_SOCKET,SO_BROADCAST,true);
if($opt_ret <0) {
echo "setsockopt() failed, error: " . strerror($opt_ret) ."\n";
return false;
}
if(socket_sendto($s, $msg, strlen($msg), 0, $addr,$socket_number)) {
socket_close($s);
return true;
}
else {
return false;
}
}
}