software development


I am trying to implement a wake-on-lan functionality with PHP. To do so, I am required to compile my own version of PHP with sockets enabled, which I have done to the best of my ability. However, it seems that when I run my code from another computer and the target computer is in standby/off, I get a 500 server error. If the target computer is on, I get the message telling me the magic packet was sent successfully. NOTE: Despite this 500 error, the code seems to still occasionally wake my computer up!! I am baffled as to what the problem is. My code is enclosed below. Ignore the bit about no-ip.

Much thanks!!

<? # Wake on LAN - (c), upgraded by Murzik # Modified by Allan Barizo flush(); function get_web_page( $url ) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => true, // return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 10, // stop after 10 redirects ); $ch = curl_init( $url ); curl_setopt_array( $ch, $options ); $content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); print($header[0]); return $header; } function WakeOnLan($addr, $mac,$socket_number) { $addr_byte = explode(':', $mac); $hw_addr = ''; for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a])); $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255); for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr; // send it to the broadcast address using UDP // SQL_BROADCAST option isn't help!! $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 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); 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)) { echo "Magic Packet sent successfully!"; socket_close($s); return TRUE; } else { echo "Magic packet failed!"; return FALSE; } } } // MAC Address of the listening computer's network device $mac_addy = "00:15:c5:1f:00:c4"; // Extract info from $thisurl = ""; $myUrlInfo = get_web_page( $thisurl ); $url = $myUrlInfo["url"]; $url = explode("http://",$url); $url = explode(":",$url[1]); //$ip_addy = $url[0]; //$socket_number = $url[1]; // Use these for now $ip_addy = "IP Here"; $socket_number = "Port Here"; echo "$ip_addy:$socket_number"; echo " "; WakeOnLan($ip_addy, $mac_addy,$socket_number) ?>


I should add that another script I have to display version information works regardless of what state my computer is in.


Have you tried pulsing it? Say once every 250ms for a second or 2.

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


Not sure what you mean. Do you mean pulsing the magic packet? If so, no I haven’t. However, I don’t think this is the problem; when I execute the script, a packet sniffer on the target computer receives the packet every time. My problem is that I get that 500 error when the target computer is in standby and I have NO idea why…the php script should be independent of the state of target computer, right?!

Thanks for your help, sXi.


I was considering it could be more a hardware issue, as you mentioned intermittent behaviour.

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost