Search code examples
phpwake-on-lan

trying to make simple WakeOnLan script with PHP


Trying to make a little script that will turn on server. I found few examples on the net, but wanted to keep this basic/simple also to get better hold of how it all fits together. But this doesn't work, I realize I've to specify subnet 255 255 255 0 somewhere...

any ideas?

<?php

//check if server is up and running
$alive = fsockopen("XXX.168.1.1", 80, $errno, $errstr, 2);

    if (!$alive) {
        echo "<h1>Server is Down!</h1>";
        echo "I will try to turn it on now...";

        //Creating magic packet
        $mac_address = str_repeat("XXX5XXXX5XXX", 16);
        $msg = "FFFFFFFFFFFF " . "$mac_address" . "000000000000";
        $host_addr = "XXX.168.1.1";
        $host_port = "X";

        //Connect send and close connection
        $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
        $socket_data = socket_send($socket, $msg, strlen($msg), 0, $host_addr, $host_port);

        socket_close($socket);
        //testing
        //echo 
    } else {
        echo "<h1>Server is Up!</h1>";
        fclose($alive);
    }

?>

Solution

  • Check this: http://www.php.net/manual/en/function.socket-sendto.php#57746

    And this: http://www.php.net/manual/en/function.socket-send.php#58574

    Maybe even this: http://www.codeproject.com/Articles/11469/Wake-On-LAN-WOL

    You'll find out that you need to set the ip address argument to '255.255.255.255' to make a broadcast :)