Zend - The PHP Company




External

Add Code


Magic Packet Class  

Type: class
Added by: MrExpert2001
Entered: 06/06/2002
Last modified: 06/12/2001
Rating: - (fewer than 3 votes)
Views: 9515
This class allows you to wake up/power on a computer remotely.


<?php

    
/***************************************************
     * PHP Class for UDP Magic Packet Wake-on-Lan       
     * �2002                                            
     ***************************************************
     * 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;
    }
}

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories