# How to wake up a pc by PHP

- URL: https://wuming.si/blog/1160/
- Author: Wuming
- Published: 2016-04-18
- Language: en
- Categories: Raspberry Pi, Technology

source: 
    http://php.net/manual/en/ref.sockets.php
    http://www.rkrishardy.com/2009/12/permission-denied-13-when-opening-socket-in-php-apache/
    
    <?php
    /**
     * Wake-on-LAN
     *
     * @return boolean
     *   TRUE:    Socked was created successfully and the message has been sent.
     *   FALSE:   Something went wrong
     *
     * @param string|array  $mac   You will WAKE-UP this WOL-enabled computer, you
     *                             need to add the MAC-address here. Mac can be
     *                             array too.
     *
     * @param string|array  $addr  You will send and broadcast to this address.
     *                             Normally you need to use the 255.255.255.255
     *                             address, so I made it as the default. You don't need to do anything with this.
     *                       
     *                             If you get permission denied errors when using
     *                             255.255.255.255 have permission denied problems
     *                             you can set $addr = false to get the broadcast
     *                             address from the network interface using the
     *                             ifconfig command.
     *
     *                             $addr can be array with broadcast IP values
     *
     * Example 1:
     *   When the message has been sent you will see the message "Done...."
     *   if ( wake_on_lan('00:00:00:00:00:00'))
     *      echo 'Done...';
     *   else
     *      echo 'Error while sending';
     */
    
    function wake_on_lan($mac, $addr=false, $port=7) {
        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($addr); $i++)
                if ($addr[$i] !== false) {
                    $last_ret = wake_on_lan($mac, $addr[$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-address 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;  
    }
    
    if (@wake_on_lan('00:00:00:00:00:00')) {
        echo 'Done...';
    } else {
        echo 'Error while sending';
    }
    ?>
