Wake on Lan , working ok without configurations, and some features
<?php
function wake_on_lan($mac,$addr=false,$port=7) {
if ($addr===false){
exec("ifconfig | grep Bcast | cut -d \":\" -f 3 | cut -d \" \" -f 1",$addr);
$addr=array_flip(array_flip($addr));
}
if(is_array($addr)){
$last_ret=false;
for ($i=0;$i<count($ret);$i++)
if ($ret[$i]!==false)
$last_ret=wake_on_lan($mac,$ret[$i],$port);
return($last_ret);
}
if (is_array($mac)){
$ret=array();
foreach($mac as $k=>v)
$ret[$k]=wake_on_lan($v,$addr,$port);
return($ret);
}
$mac=strtoupper($mac);
if (!preg_match("/([A-F0-9]{1,2}[-:]){5}[A-F0-9]{1,2}/",$mac,$maccheck))
return false;
$addr_byte = preg_split("/[-:]/",$maccheck[0]);
$hw_addr = '';
for ($a=0; $a < 6; $a++)$hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = str_repeat(chr(255),6);
for ($a = 1; $a <= 16; $a++)
$msg .= $hw_addr;
if (function_exists('socket_create')){
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if ($sock){
$sock_data = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); if ($sock_data){
$sock_data = socket_sendto($sock, $msg, strlen($msg), 0, $addr,$port); if ($sock_data){
socket_close($sock); unset($sock);
return(true);
}
}
}
@socket_close($sock);
unset($sock);
}
$sock=fsockopen("udp://" . $addr, $port);
if($sock){
$ret=fwrite($sock,$msg);
fclose($sock);
}
if($ret)
return(true);
return(false);
}
?>