<?php 

    /*************************************************** 
     * PHP Class for UDP Magic Packet Wake-on-Lan        
     * ©2002 http://www.zend.com/codex.php?id=927&single=1
     *************************************************** 
     * This class exports 2 functions you would most     
     * probably want to use:                             
     *    void MagicPacket() - class descriptor          
     *    bool wake(string $ip, string $mac[, int $port]) - to wake the computer at the specified 
     *                           ip address. The mac argument is the MAC 
     *                           address of the NIC you would like to 
     *                           wake up. The port argument is any UDP port, 
     *                           preferably 9, the UDP discard port. 
     *                           Returns true on success, false on failure. 
     *************************************************** 
     * Usage: $wol = new MagicPacket();                  
     *        $wol -> wake('192.168.1.201','001122334455', 9);             
     ****************************************************/ 

class MagicPacket 
{ 
    function MagicPacket() 
    { 
        $this -> $ff = chr(0xFF); 
    } 

    function wake($ip, $mac, $port) 
    { 
        $this -> $nic = fsockopen("udp://" . $ip, $port); 
        if( !$this -> $nic ) 
        { 
            fclose($this -> $nic); 
            return false; 
        } 
        else 
        { 
            fwrite($this -> $nic, $this -> generate_magic_packet($mac)); 
            fclose($this -> $nic); 
            return true; 
        } 
    } 

    function generate_magic_packet($dest_mac) 
    { 
        $packet = ""; 
        for($i = 0; $i < 6; $i++) 
        { 
            $packet .= $this -> $ff; 
        } 
        for($i = 0; $i < 6; $i++) 
        { 
            $packet .= chr((int)substr($dest_mac, $i, $i + 2)); 
        } 
        return $packet; 
    } 
} 

?> 