Voting

: max(six, three)?
(Example: nine)

The Note You're Voting On

roberto at spadim dot com dot br
18 years ago
Wake on Lan , working ok without configurations, and some features

<?php
function wake_on_lan($mac,$addr=false,$port=7) {
//Usage
// $addr:
// You will send and broadcast tho this addres.
// Normaly you need to use the 255.255.255.255 adres, so i made it as default. So you don't need
// to do anything with this.
// Since 255.255.255.255 have permission denied problems you can use addr=false to get all broadcast address from ifconfig command
// addr can be array with broadcast IP values
// $mac:
// You will WAKE-UP this WOL-enabled computer, you need to add the MAC-addres here.
// Mac can be array too
//
//Return
// TRUE: When socked was created succesvolly and the message has been send.
// FALSE: Something went wrong
//
//Example 1
// When the message has been send you will see the message "Done...."
// if ( wake_on_lan('00:00:00:00:00:00'))
// echo 'Done...';
// else
// echo 'Error while sending';
//
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);
}
//Check if it's an real MAC-addres and split it into an array
$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]);

//Creating hardware adress
$hw_addr = '';
for (
$a=0; $a < 6; $a++)//Changing mac adres from HEXEDECIMAL to DECIMAL
$hw_addr .= chr(hexdec($addr_byte[$a]));

//Create package data
$msg = str_repeat(chr(255),6);
for (
$a = 1; $a <= 16; $a++)
$msg .= $hw_addr;
//Sending data
if (function_exists('socket_create')){
//socket_create exists
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); //Can create the socket
if ($sock){
$sock_data = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); //Set
if ($sock_data){
$sock_data = socket_sendto($sock, $msg, strlen($msg), 0, $addr,$port); //Send data
if ($sock_data){
socket_close($sock); //Close socket
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);
}
?>

<< Back to user notes page

To Top