I am an IT Helpdesk with very little PHP experience. Now I have a project in the company to let a colleague connect to the company's intranet using a VPN, and then remotely wake up his computer at the company. So my idea is to set up a PHP-based website in the company, let a colleague enter his computer's MAC address, and then he can wake up his computer remotely after submission. I found the following code on the Internet, but there will be errors when applying it. error message is bleow
- Warning: Undefined array key 1 in C:\xampp\htdocs\wol\wol.php on line 41
- Warning: Undefined array key 2 in C:\xampp\htdocs\wol\wol.php on line 41
- Warning: Undefined array key 3 in C:\xampp\htdocs\wol\wol.php on line 41
- Warning: Undefined array key 4 in C:\xampp\htdocs\wol\wol.php on line 41
- Warning: Undefined array key 5 in C:\xampp\htdocs\wol\wol.php on line 41
- magic packet Failed to send!
Can you help me? Thanks in advance.
wol.php
<?php
/**
* Implement Wake-on-LAN function
*/
class WOL
{
private $hostname; // The hostname of the wake-up device
private $mac; // The mac address of the wake-up device
private $port; // The port of the wake-up device
private $ip; // The IP address of the wake-up device (not necessary, the program will automatically obtain the corresponding ip according to $hostname)
private $msg = array(
0 => "The target machine is already awake.",
1 => "socket_create execution failed",
2 => "socket_set_option execution failed",
3 => "magic packet Sent successfully!",
4 => "magic packet Failed to send!"
);
function __construct($hostname,$mac,$port,$ip = false)
{
$this->hostname = $hostname;
$this->mac = $mac;
$this->port = $port;
if (!$ip)
{
$this->ip = $this->get_ip_from_hostname();
}
}
public function wake_on_wan()
{
if ($this->is_awake())
{
return $this->msg[0]; // If the device is already awake, no other operations will be performed
}
else
{
$addr_byte = explode(':', $this->mac);
$hw_addr = '';
for ($a=0; $a<6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a=1; $a<=16; $a++) $msg .= $hw_addr;
// Send data packets via UDP
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false)
{
return $this->msg[1]; // socket_create Execution failed
}
$set_opt = @socket_set_option($s, 1, 6, TRUE);
if ($set_opt < 0)
{
return $this->msg[2]; // socket_set_option Execution failed
}
$sendto = @socket_sendto($s, $msg, strlen($msg), 0, $this->ip, $this->port);
if ($sendto)
{
socket_close($s);
return $this->msg[3]; // magic packet Sent successfully!
}
return $this->msg[4]; // magic packet Failed to send!
}
}
private function is_awake()
{
$awake = @fsockopen($this->ip, 80, $errno, $errstr, 2);
if ($awake)
{
fclose($awake);
}
return $awake;
}
private function get_ip_from_hostname()
{
return gethostbyname($this->hostname);
}
}
?>
index.php
<?php
include("wol.php");
$WOL = new WOL("255.255.255.255","38-D5-47-AA-69-A2","9");
$status = $WOL->wake_on_wan();
echo $status;
?>