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.

Subscribe by RSS Feed
Stumble it!
Furl This!
Reddit!
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 ??
September 3rd, 2008 at 8:24 pm
On my (I think default) install of php under xp the sockets extension wasn't enabled. It worked great once I figured it out. Thanks!
October 1st, 2008 at 8:26 am
Hi, just want to know what if I dont want to Power Up a pc, lets say I want to Power Off my computer, so whats the magic packet for this?, thanks
October 1st, 2008 at 4:10 pm
Hi there nice post but i have 2 Questions and one problem
1st : It didn`t work with me at all i tryed everything u said and nothing work.
2nd : my pc behind router and i want to wake them up from my home
so how i get the internet IP Coz when when i get it if i restart the router or the elc get off and back again the internet IP will change
and if i sent the broadcast as u said
I know that my company that give me the DSL line use the same broadcast to every member it have
so when i sent the magic packet it will search all over the internet to find the right mac add???
Please help me here
P.S Sorry for my english and Sorry coz i`m noob
October 13th, 2008 at 2:44 am
-include sockets PHP extension in PHP.ini:
extension=php_sockets.dll
-be sure to have shorttags enabled in PHP.ini:
short_open_tag = On
-change line
$opt_ret = socket_set_option($s, 1, 6, TRUE);
into
$opt_ret = socket_set_option($s, SOL_SOCKET , SO_BROADCAST, 1);
-forward UDP port 9 in your router
and it works like a charm! Thanks!
October 24th, 2008 at 1:08 am
An update:
It only worked for some hours.
First I tried to forward the port to .255 as suggested at the end of the article. Tomato firmware doesn\\\\\\\\\\\\\\\'t allow this.
Then I found a new way:
1) Pick an unused IP address for your LAN (I used 192.168.1.250)
2) Set up a port forward form a port of your choice to this address
3) Add a static ARP entry that maps this address to a broadcast address by adding the following line e.g. for the Tomato firmware it\\\\\\\\\\\\\\\'s under Administration/Scripts/Firewall:
ip neighbor add 192.168.1.250 lladdr ff:ff:ff:ff:ff:ff dev br0 nud permanent
Now it works always! Credits go to http://www.perculasoft.com/sleepover/routers.html
March 13th, 2009 at 8:36 am
hello,
i m having some problems with wol.
The wol is working fine in LAN mode.
But when i use the php script it is not working.
The pc is not booting up.
is there anything that i can do to fix it?
The php script says Magic Packet sent successfully!
but pc is not booting
Ip 192.168.2.20 is in dmz and portforwaring UDP 9 to 192.168.2.20 Still not working.
need some help
thank u
March 28th, 2009 at 2:31 pm
Hi,
thanks very much for this script - it works perfectly and is especially useful for me because where I work the proxy server stops me sending magic packets on port 9. This solution means I can start my home server form behind that proxy. Luckily they didn\'t block port 22 so I can shell into it and work on my own projects when the boss is out of the office!
I wrote a tutorial on how to set the whole thig up here: http://blog.xoundboy.com/?p=196
I used your script. Hope you don\'t mind.
cheers
Ben
March 29th, 2009 at 2:43 pm
Ben, I'm glad the script worked out well for you and I'm honored to be included in your extensive tutorial blog post. Be careful with that port 22 especially if your home computer is on a static IP. in my experience that port is highly targeted by crackers.
April 1st, 2009 at 7:24 am
And because it is targeted by crackers (bots), good practice is to disable the password authentication in SSHd. I use private key authentication ONLY. You can still login with password from console.
Tom
April 15th, 2009 at 2:49 pm
Thanks for this great script! It work for some months for me with the change to "$opt_ret = socket_set_option($s, SOL_SOCKET , SO_BROADCAST, 1);" but now it has stopped working since some other months.
Do anyone else have this problem? Nothing is changed and Depicus work fine.
/Paola
May 27th, 2009 at 9:59 am
If your computer is behind a rooter,first how do you remove the NAT obstacole to be sure that is working....:|
May 1st, 2010 at 4:29 am
[...] Hacker not Cracker - Wake-On-LAN Tutorial with bonus PHP script [...]
September 18th, 2010 at 4:24 pm
[...] se passe ici grâce à la fonction php découverte sur Hacker not Cracker Cette entrée a été publiée dans Informatique, avec comme mot(s)-clef(s) Wake on lan. Vous [...]
October 15th, 2010 at 11:40 pm
Worked for me - but I had to change the last part of the IP address to 255. This is at an educational institution where the machines have unique internet IPs. I can turn my machine on with another machine on the same subnet, but not from the outside.
Thanks for the script!
November 21st, 2010 at 6:47 pm
[...] es mal mit einem WOL-script: siehe hier: http://www.hackernotcracker.com/2006...hp-script.html Wenn das Skript funktioniert kopiere einen "link" in deinen Windows Autostart [...]
April 7th, 2011 at 3:48 pm
[...] mutualisé ces scripts sont inopérants. Le seul script efficace que j’ai trouvé sur un site américain fonctionne même sur les pages persos Free.fr, réputées pour être [...]
August 21st, 2011 at 12:11 pm
[...] I got it from here: [...]
September 20th, 2011 at 9:51 pm
Useful info here. To answer website analayzer's question (comment 41), to get around a NAT you would need to use port forwarding on your router handling the NAT to forward the packet to the machine.
May 7th, 2013 at 2:24 am
GaqJlt The Slack longchamp le pliage snuck up on!
EvzEpi NeiAqq montres Michael Kors Purgesac ¨¤ main longchamp WniAqf achat sac longchamp PplPge sac guess pas cher IenOft Permanently sac longchamps YvsQyh isabel marant solde unite TijEbk
Function and fashion are the most important features of a good bag. longchamp bags The materials used for fabricating hand bags, shoulder bags, saddle bags or any type of bags are various. Chaussures Isabel Marant The most appreciated fabrication material is leather. Leather saddle bags and leather shoulder bags can be found in various styles sacs guess When you purchase leather shoulder bags and leather saddle bags you will benefit from a fashionable product Isabel Marant sneakers There are three main categories of leather saddle bags and leather shoulder bags sac longchamp
it is still important to look out at a reputable store Burberry bags Every woman love to wear trendy handbags which suit their dress Cheap Longchamp we manufacture our bags by keeping one think in mind that your choice and latest trend should be there in genuine rates sac a main michael kors generally woman wear an handbags which complimenting with their dress Sac longchamp You have an opportunity to get an handbag for every occasion like for parties Sac Guess
PpaMen XviHwb longchamp sacs soldes AlaNtu GtsLv magasin guess aDuzAxd IeeWr isabel marant sneakers HgmFnz VieFuq pliage longchamp