//The Wake On LAN part - Attempts to send the wake on LAN (magic) packet
function TryToSendPacket() //Returns success or error message
{
//Get passed variables
global $Ready, $port, $mac, $ip;
$Ready=isset($_REQUEST['port']) && isset($_REQUEST['mac']) && isset($_REQUEST['ip']);
$port=isset($_REQUEST['port']) ? trim($_REQUEST['port']) : '9';
$mac=isset($_REQUEST['mac']) ? trim($_REQUEST['mac']) : '';
$ip=isset($_REQUEST['ip']) ? trim($_REQUEST['ip']) : '';
if(!$Ready) //User hasn't submitted form yet
return '';
//Confirm the server supports socket functions
if(!function_exists('socket_create'))
return 'PHP server does not have the socket extension.';
//Confirm the IP Address
$MyIP=gethostbynamel($ip);
if($MyIP===FALSE)
return 'Invalid IP address or domain name passed.';
$ip=$MyIP[0];
//Confirm the port
if(preg_match('/[^0-9]/', $port) || !strlen($port))
return 'Port number must be a positive integer.';
//Prepare mac address
if(!preg_match('/^([0-9A-F]{2}[^0-9A-F]){5}[0-9A-F]{2}$/i', $mac)) //Confirm mac address is valid
return 'The mac address must be 6 hex digits long seperated by a single non hexedecimal character, e.x. 00:01:02:30:AB:FF .';
$AddrHex=preg_split('/[^0-9A-G]/i', $mac); //Split up mac address into bytes
$mac=implode(':', $AddrHex); //Redo the mac address into a proper format
$MacAddr="";
for($i=0;$i<6;$i++) //Turn the hex strings into characters
$MacAddr.=chr(hexdec($AddrHex[$i]));
//Prepare the magic packet - http://en.wikipedia.org/wiki/Magic_packet#Magic_Packet
$FortyEight1s=str_repeat(chr(255), 6); //48 binary 1s in a row
$Msg=$FortyEight1s.$MacAddr.chr(0x8).chr(0x42).$FortyEight1s; //This specific magic packet format seems to work best
for($i=0;$i<16;$i++)
$Msg.=$MacAddr;
//Send the magic packet
//Create the UDP socket
$s=socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
if($s===false)
return 'Error creating socket!
Error code is "'.socket_last_error($s).'" - '.socket_strerror(socket_last_error($s));
//Set the socket to broadcast
$opt_ret=socket_set_option($s,SOL_SOCKET,SO_BROADCAST,1);
if($opt_ret<0)
return 'setsockopt() failed, error: '.strerror($opt_ret);
//Send the packet
$e=socket_sendto($s,$Msg,strlen($Msg),0,$ip,$port);
socket_close($s);
//Confirm correct packet
if($e!=116) //Confirm 116 bytes were sent
return "Packet may not have sent correctly. ($e)"; //Return how many bytes were sent
//Return success
return "$ip of MAC $mac should now boot through UDP port $port if it supports Wake On LAN.";
}
//The rest is the client GUI
?>
Dakusan's Wake On LAN information