note 73162 added to ref.sockets
| From: | roberto at spadim dot com dot br | Date: | Sun, 11 Feb 2007 18:27:54 +0000 |
| Subject: | note 73162 added to ref.sockets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123256@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 201.68.31.154
----
Manual Page -- http://www.php.net/manual/en/ref.sockets.php
Edit -- https://master.php.net/note/edit/73162
Del: integrated -- https://master.php.net/note/delete/73162/integrated
Del: useless -- https://master.php.net/note/delete/73162/useless
Del: bad code -- https://master.php.net/note/delete/73162/bad+code
Del: spam -- https://master.php.net/note/delete/73162/spam
Del: non-english -- https://master.php.net/note/delete/73162/non-english
Del: in docs -- https://master.php.net/note/delete/73162/in+docs
Del: other reasons-- https://master.php.net/note/delete/73162
Reject -- https://master.php.net/note/reject/73162
Search -- https://master.php.net/manage/user-notes.php