WOL: Wake-on-LAN Tutorial with Bonus PHP Script
I love tweaking my computer! Every opportunity that allows me to specify what I want makes me excited of the possibilities. (With the exception of Azureus. That's just too confusing for me!) In fact, the first thing I check after installing a new program is the possible existence of "Options," "Preferences," "Tools," "Configuration," "Settings," or "Setup!"
BIOS (or CMOS)
When I first became involved with computers, I started to mess around with the BIOS. It was an intriguing little 16-color control panel. I'm talking about those settings that you can change after pressing "F1," "F2," or "Delete" before the operating system starts to load.

Mysterious Wake on LAN Setting
I pretty much understood all the settings and what each one did except for this strange "Remote Wake-on-LAN." It wasn't until much later I discovered that initiating this option allows the computer to boot upon command from the network. Personally, that is ultra cool! But at the time, I really didn't have a use for it. Then came college.
College Needs
I increasingly saw a need to get a laptop. There were countless times when I needed to access my computer while on campus. Sadly, the only computers that I could use were the ones in the computer labs. I came up with a nifty idea that wouldn't cost a dime. Since I can't leave my computer on all day, why don't I use the Wake-on-LAN function to turn it on remotely and then implement a remote access setup. It would enable me to access my computer from anywhere regardless of whether it was off or on!
How it Works
Wake-on-LAN gives me the impression of an undocumented obscure little function that hardly anyone makes use of. Really, it is so simple that the lack of documentation is understandable. Essentially, enabling this function allows the network device to stay half-awake while the rest of the computer is off. It consumes a miniscule amount of electricity while constantly listening to the network. The network device waits until it receives a "magic packet" (seriously!). A magic packet is a small bundle of data consisting of the bytes "FF FF FF FF FF FF" followed by 16 repetitions of the listening network device's MAC address.
Creating the "Magic Packet"
A magic packet is fairly easy to generate. Depicus provides great freeware tools that allow you to input the parameters. Personally, I PHP programmed my server to generate the packet. I found some useful PHP WOL code from the PHP website. I modified it so that it will fit my liking. Just change the lines in the end.
-
<?
-
# Wake on LAN - (c) HotKey@spr.at, upgraded by Murzik
-
# Modified by Allan Barizo http://www.hackernotcracker.com
-
function WakeOnLan($addr, $mac,$socket_number) {
-
$hw_addr = '';
-
for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
-
// send it to the broadcast address using UDP
-
// SQL_BROADCAST option isn't help!!
-
if ($s == false) {
-
echo "Error creating socket!\n";
-
return FALSE;
-
}
-
else {
-
// setting a broadcast option to socket:
-
if($opt_ret <0) {
-
return FALSE;
-
}
-
echo "Magic Packet sent successfully!";
-
return TRUE;
-
}
-
else {
-
echo "Magic packet failed!";
-
return FALSE;
-
}
-
-
}
-
}
-
// Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9.
-
$socket_number = "7";
-
// MAC Address of the listening computer's network device
-
$mac_addy = "00:12:4G:SF:12:13";
-
// IP address of the listening computer. Input the domain name if you are using a hostname (like when under Dynamic DNS/IP)
-
-
WakeOnLan($ip_addy, $mac_addy,$socket_number)
-
-
-
?>
Common Hurdles
There are a couple of common problems computer users come across when trying to implement Wake-on-LAN.
- Physical Network Layer - To my knowledge, WOL will only work with Ethernet. Sorry! No Wi-fi! (Though, I heard that there are some very rare exceptions!)
- Incompatible BIOS or network device - Sometimes the BIOS or the Ethernet Device will not support WOL. These are the two main components of Wake-on-LAN. The Ethernet detects the "magic packet" and the BIOS turns the computer on.
- Network Infrastructure - In order for WOL to work, the "magic packet" must go through a direct, IP-address-specific route. That means if there are any routers or if your computers on the network share a public IP address then you'll need to do some port mapping when send magic packet via the Internet.
Last Tips
If you're still having trouble with WOL, try sending the magic packet to the broadcast address of the network that the listening computer is a part of. The broadcast address is the IP address prefix with 255 appended at the end. Mine is 192.168.0.255. (Just a little FYI: Everything that is directed to the broadcast address goes to every computer on the network.) Good luck!
Update: Welcome to all of the digg.com users! Thanks for digging and for coming to my site.

April 25th, 2006 at 6:41 am
hi, i tried this one, but for me it doesnt work.
when i save it as .php (there is missing “
April 27th, 2006 at 9:47 pm
okay i found the problem. on line 8, make sure the quotes are straight not “curly.” if neccessary, replace all curly quotes in the code with straight generic ones. i guess its the way my blog platform renders the code onto the browser. sorry!
May 5th, 2006 at 10:13 pm
when i run your code wake on lan, it gives the warning below:
Warning: socket_set_option() [function.socket-set-option]: unable to set socket option [0]: An invalid argument was supplied. in D:\xampp\htdocs\sigmatech_v1.3\sigmatech\Offshore Software Development\attendence_payroll_v1.3\design\wake-on-lan.php on line 29
Magic Packet sent (102) to 127.0.0.1
wht should i do?
thanks for your tutorials.
Hasan.
May 12th, 2006 at 10:03 am
If you want to fix the bad parameter problem, change TRUE to 1 in the socket_set_option function.
To be frank with you, you don’t code like you know how broadcast packets work. You should only need the MAC address.
Here’s some code I wrote that works perfectly:
May 12th, 2006 at 10:05 am
http://phrackattack.net/sources/wakeonlan.txt
May 12th, 2006 at 5:59 pm
I misspoke when I said to replace TRUE with 1. I meant change the other values to SOL_SOCKET and SO_BROADCAST, respectively.
May 16th, 2006 at 7:55 pm
Like I said, I got some of this code from the PHP website so it isn’t entirely mine. This is because coding for sockets is a bit difficult for me to grasp. I’ll definatly try the code that your provided. Thank you for your feedback! BTW, cool website. I really love your ASCII art concept!
June 21st, 2006 at 4:42 pm
kaskdj sjlkasd askjdflkjsa dlakjsd
July 18th, 2006 at 2:28 am
nice little script
[LowLoad.co.uk] - UK Dedicated Servers
September 5th, 2006 at 1:19 pm
I’m looking to send a WOL packet that has the PC run a command as soon as it starts up… something handy, like, say, Scandisk.
September 10th, 2006 at 5:52 pm
there are many things you can do. assuming that you are using windows, you can do a scheduled task (control panel), a startup process (put a shortcut in the startup folder in the programs folder), add a command to the registry here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run.
basically anything that you tell windows to run at startup will eventually by executed by your computer after recieving a magic packet
September 11th, 2006 at 7:43 am
I know all of those - and they all require Windows to be running before they work. I was asking if there was any way to run a dos level - like scandisk - using the WOL magic packet to trigger it.
September 11th, 2006 at 10:00 am
just edit the autoexec.bat file in the C:\ directory. and add the line “scandisk /all”
everytime before windows starts, it will do a full scandisk. I hope this answers your question!
January 4th, 2007 at 2:17 pm
“undocumented”? “obscure”? nonsense! autoritative whitepaper - http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/20213.pdf
port number doesnt really matters for target machine (that 1..50000 range is for shamans) echo 7/udp or *better* discard 9/udp is used to minimize disturbance of other hosts in the target broadcast domain.
about broadcast vs unicast. unicasting WOL packet probably will not work at all (ARP is down while machine sleeps). for the same broadcast domain
should send() WOL packet to 255.255.255.255 and in case of different broadcast domain (must have route to there, allowed directed broadcasts, etc)
should send to broadcast address of target subnet (all 1’s in host part)
April 26th, 2007 at 1:12 pm
Here’s a freeware that does the same from a Java enabled mobile phone
http://thinkabdul.com/2007/04/26/j2me-wakeonlan-client-for-java-mobile-phones-wakeupbootuppower-on-computer-remotely-behind-router-from-mobile-device-via-gprsedge3g/
May 26th, 2007 at 4:24 am
If somebody didn’t understand what “phrackattack” sad, the line 21: $opt_ret = socket_set_option($s, 1, 6, TRUE);
should be changed to:
$opt_ret = socket_set_option($s, SOL_SOCKET, SO_BROADCAST, TRUE);
July 11th, 2007 at 3:12 pm
any way of implementing tis on a website to log on via html/flash page?
December 6th, 2007 at 11:25 am
I don’t understand it. How do I use this code (execute it) ?
December 20th, 2007 at 12:16 pm
I keep getting Call to undefined function socket_create();
IIS 6.0(I think)
PHP 5.2.5
(on my local machine)
have all extensions loaded; and also tried enabling extension=php_sockets.dll
also made sure extension_dir was right and restarted the IIS.
I am revisiting PHP after a long time & I used the php-5.2.5-win32-installer.msi to install PHP.
Please advise!
December 29th, 2007 at 2:26 am
The socket socket_create() tells PHP to do exactly that. However inorder to do that PHP has to be configured/compiled with the sockets enabled.
I am running into an error “socket_sendto() unable to write to socket [1]: Operation not permitted in line 26″.
Operation not permitted?? How do I permit it?
DPC
January 15th, 2008 at 5:12 pm
This reply goes to Pisosse’s question, though coming a little late (6 months later!), I hope that it would be useful to somebody
.
Copy+paste the code to a php file, set the MAC and IP addresses where you’re sending the Packet, and run the php by command-line or by http-request.
A requirement that I didn’t see on ANY forum is that before letting PHP try to send the packet, your OS has to got an entry for the PC’s MAC address on it’s ARP table. That would be, if you’re trying to send the packet to a PC with IP 200.9.5.130 and MAC 01:02:03:04:05:06 from a Linux PC, first you should run the command: “arp -s 200.9.5.130 01:02:03:04:05:06″, or otherwise add the following line to the /etc/ethers file: “01:02:03:04:05:06 200.9.5.130″, so Linux would be able to locate the MAC address for the host you’re trying to talk to.
Another way I found to send the magic packet without having to explicitly add the ARP-line to your ARP-table or the ethers file, is to send the packet to an IP-broadcast-address on your network (preferably not 255.255.255.255 for the traffic it would generate, but it will do too), so your OS will not be guessing the MAC address before sending the packet, and therefore send it right away.
Cheers from Paraguay!
(jjbareiro@gmail.com)
January 18th, 2008 at 1:39 pm
Well done!
Worked first time “out of the box”
Time from Google->waking up my PC was less than 5 mins from start to finish.
Thanks.
April 7th, 2008 at 7:34 am
Did it under 2min!
April 13th, 2008 at 8:59 pm
Great job on this. Works fine on my personal server. However, on a shared sever I also get the
socket_sendto() “unable to write to socket [1]: Operation not permitted” error. Any way around this?
I know depiscus has a COM / dll extension for php that supposedly works on Windows servers, but I can’t figure that one out either. Seems more complicated and rather large (300kb) just to send a packet . . . http://www.depicus.com/wake-on-lan/wake-on-lan-com.aspx
June 13th, 2008 at 4:48 am
How can I set up WOL?
July 25th, 2008 at 11:03 am
since a turned-off doesn’t have an IP, how can u reach it?
July 25th, 2008 at 12:17 pm
Excelent script but I’ve a problem to use
Warning: socket_sendto() unable to write to socket [1]: Operation not permitted
How can i fix it?
See you,
Tomás form Argentina
July 26th, 2008 at 11:35 am
You use the broadcast address (example 192.168.1.255) which will be sent to all nodes in a subnet. Basically, use the network prefix (192.168.1) with “.255″ appended.
July 26th, 2008 at 11:36 am
Tomas, your hosting provider either blocked that function or blocked that port. Contact them to open the function and the port you plan to use.
August 11th, 2008 at 1:00 am
my target machine sits behind a router, like most machines, right?. the router folks say its always illegal to use addr ending in .255 like in forwarding a magic packet traffic to port 9, etc. - i have tried with dlink 624I and linksys wrt150n. these are pretty common for home or small biz use. i’m missing something here? how do you get the magic packets addressed to x.x.x.255 if no routers will alllow a pinhole/port fwd configuration using that address ??