在區域網路中,可以傳送 Magic Packet 給主機的網路卡,Magic Packet 內容開頭有6個 "FF",再接 16 個網路卡 MAC,當主機版有開啟支援 Wake on LAN 功能,網路卡接收到符合條件的 Magic Packet,就能開啟電腦。(每個主機版關於 Wake on LAN 的名稱不盡相同,例如 PME Event Wake Up、Power On By PCI Devices...)
另一個影響能否網路開機的設定,在作業系統裡面裝置管理員,網卡裝置內容裡的電源管理,需勾選「允許這個裝置喚醒電腦」。
假設網路卡 MAC 為:01-02-03-04-05-06
Magic Packet 內容則為:
FFFFFFFFFFFF010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506010203040506
然後以 UDP 通訊協定傳送給電腦的 port 7、或 port 9
所以實現網路開機的方式,就是在區網內想辦法傳送給目標電腦網卡 Magic Packet,如果用來傳送 Magic Packet 的設備(Server),平常也有對外連線,也可達成在外網開機的效果。
以下 PHP 傳送 Magic Packet 範例來源
WOL: Wake-on-LAN Tutorial with Bonus PHP Script
僅稍微修改後如下:
class WakeOnLan { /** * 喚醒電腦 * @param type $addr ,目標IP 或 廣播位址(格式 01-02-03-04-05-06 或 01:02:03:04:05:06) * @param type $mac * @param type $port , 7 or 9 * @return boolean */ public function wake($addr, $mac, $port) { $mac = str_replace("-", ":", $mac); $addr_byte = explode(':', $mac); $hw_addr = ''; for ($a = 0; $a < 6; $a++) { $hw_addr .= chr(hexdec($addr_byte[$a])); } // 開頭六個 "FF" $msg = chr(255) . chr(255) . chr(255) . chr(255) . chr(255) . chr(255); // 16個MAC for ($a = 1; $a <= 16; $a++) { $msg .= $hw_addr; } // 開一個 UDP 的 socket // AF_INET:IP4 // SOCK_DGRAM:The UDP protocol is based on this socket type // SOL_UDP:使用 UDP 通訊協定 $skt = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $err = array(); if ($skt === false) { $err[] = "Error creating socket!"; $err[] = "Error code is '" . socket_last_error($skt) . "' - " . socket_strerror(socket_last_error($skt)); throw new Exception(implode("\n", $err)); } else { // 設定使用broadcast廣播訊息 // $opt_ret = socket_set_option($skt, 1, 6, TRUE); $opt_ret = socket_set_option($skt, SOL_SOCKET, SO_BROADCAST, true); if ($opt_ret === false) { $err[] = "setsockopt() failed, error: " . socket_strerror(socket_last_error($skt)); throw new Exception(implode("\n", $err)); } if (socket_sendto($skt, $msg, strlen($msg), 0, $addr, $port)) { //Magic Packet sent successfully $res = trim(socket_strerror(socket_last_error($skt))); socket_close($skt); return $res; } else { $err[] = "Magic packet failed!"; throw new Exception(implode("\n", $err)); } } }}$WOL = new WakeOnLan();try { $res = $WOL->wake('192.168.0.255', "01-02-03-04-05-06", 7); $res = $WOL->wake('192.168.0.255', "01-02-03-04-05-06", 9); var_dump($res);} catch (Exception $ex) { var_dump($ex->getMessage());}
- 將 Magic Packet 指定傳給 broadcast address(例如:192.168.0.255),可傳送給區網所有電腦,MAC符合的才會開機。
- 有的電腦可能傳給 port 7 或 port 9 其中一個才會開機。
- 原範例 socket_set_option($skt, 1, 6, TRUE),1、6 改成用常數取代,因不同環境 SOL_SOCKET、SO_BROADCAST 的常數直似乎不同。查看所有 soket 常數 http://php.net/manual/en/sockets.constants.php#108291
$a = get_defined_constants(TRUE);foreach ($a['sockets'] as $constant => $value) { printf("%-25s %d", $constant, $value);}
網路喚醒 - 維基百科,自由的百科全書
設定Windows 8網路喚醒(Wake On LAN) - 黑暗執行緒
PHP: socket_create - Manual
PHP: socket_set_option - Manual
PHP: socket_get_option - Manual
arduino - PHP : Send an UDP broadcast message , and wait the response - Stack Overflow
小信豬的原始部落: [TCP/IP Illustrated] Broadcasting & Multicasting
Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
