透過 HTTP Header 觸發瀏覽器下載 `.url` (Windows) 或 `.webloc` (macOS) 檔案,使用者儲存後即為桌面捷徑。
<?php
/**
* 生成 Windows 桌面捷徑 (.url)
* @param string $url 目標網址
* @param string $filename 下載檔名 (不含副檔名)
* @param string $iconUrl 圖示網址 (可選)
* @param string $title 捷徑標題 (可選)
*/
function sendWindowsShortcut(string $url, string $filename = 'Shortcut', string $iconUrl = '', string $title = ''): void {
$filename = basename($filename);
$filename = preg_replace('/[<>:"|?*]/', '', $filename); // 移除非法字元
$filename .= '.url';
$content = "[InternetShortcut]\n";
$content .= "URL=" . $url . "\n";
$content .= "IDList=\n";
if ($iconUrl) {
$content .= "IconFile=" . $iconUrl . "\n";
$content .= "IconIndex=0\n";
}
if ($title) {
$content .= "HotKey=0\n";
$content .= "[" . $title . "]\n"; // 自定義區段
}
$content .= "Prop3=19,2\n"; // 標準屬性
// 輸出標頭
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . rawurlencode($filename) . '"');
header('Content-Length: ' . strlen($content));
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
header('Expires: 0');
echo $content;
exit;
}
// 使用範例
// sendWindowsShortcut('https://www.example.com', '我的網站', 'https://www.example.com/favicon.ico', 'Example Site');
?>
<?php
/**
* 生成 macOS 桌面捷徑 (.webloc)
*/
function sendMacWebloc(string $url, string $filename = 'Shortcut'): void {
$filename = basename($filename) . '.webloc';
$plist = '' . "\n";
$plist .= '' . "\n";
$plist .= '\n';
$plist .= '\n';
$plist .= ' URL \n';
$plist .= ' ' . htmlspecialchars($url, ENT_XML1) . ' \n';
$plist .= ' \n';
$plist .= ' \n';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . rawurlencode($filename) . '"');
header('Content-Length: ' . strlen($plist));
echo $plist;
exit;
}
?>
<?php
function sendDesktopShortcut(string $url, string $filename = 'Shortcut', string $iconUrl = ''): void {
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
// 簡易平台偵測
$isMac = stripos($ua, 'Macintosh') !== false || stripos($ua, 'Mac OS X') !== false;
$isIOS = stripos($ua, 'iPhone') !== false || stripos($ua, 'iPad') !== false;
$isWindows = stripos($ua, 'Windows') !== false;
if ($isMac || $isIOS) {
sendMacWebloc($url, $filename);
} else {
// Windows、Linux、Android、其他預設 .url
sendWindowsShortcut($url, $filename, $iconUrl);
}
}
// 入口頁面:shortcut.php?url=https://example.com&name=MySite
$url = $_GET['url'] ?? 'https://www.example.com';
$name = $_GET['name'] ?? 'Shortcut';
$icon = $_GET['icon'] ?? '';
// 基本驗證
if (!filter_var($url, FILTER_VALIDATE_URL)) {
http_response_code(400);
exit('Invalid URL');
}
sendDesktopShortcut($url, $name, $icon);
?>
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>加入桌面捷徑</title></head>
<body>
<h1>將本網站加入桌面</h1>
<p>點擊下方按鈕下載捷徑,儲存至桌面即可快速存取。</p>
<a href="shortcut.php?url=https://www.example.com&name=Example%20Site&icon=https://www.example.com/favicon.ico"
class="btn btn-primary">
📥 下載 Windows 捷徑 (.url)
</a>
<br><br>
<a href="shortcut.php?url=https://www.example.com&name=Example%20Site"
class="btn btn-secondary">
📥 下載 macOS 捷徑 (.webloc)
</a>
<script>
// PWA 安裝提示 (現代替代方案)
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
document.getElementById('pwa-install').style.display = 'block';
});
document.getElementById('pwa-install-btn')?.addEventListener('click', async () => {
if (deferredPrompt) {
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
console.log('PWA install:', outcome);
deferredPrompt = null;
}
});
</script>
<div id="pwa-install" style="display:none; margin-top:20px;">
<button id="pwa-install-btn" class="btn btn-success">📱 安裝為 PWA 應用程式</button>
<p><small>支援 Chrome/Edge/Opera,離線可用、原生體驗</small></p>
</div>
</body></html>
| 細節 | 處理方式 |
|---|---|
| 檔名編碼 | rawurlencode() 符合 RFC 5987,支援中文檔名 |
| Content-Type | application/octet-stream 強制下載而非瀏覽器開啟 |
| Content-Disposition | attachment 觸發「另存新檔」對話框 |
| 快取控制 | no-cache、must-revalidate 確保每次下載最新 |
| 輸出前無輸出 | 嚴禁 header 前有空白、BOM、echo,否則檔案損壞 |
現代瀏覽器支援「安裝為應用程式」,提供比捷徑更好的體驗:離線快取、推播通知、全螢幕、應用程式圖示。建議並行提供: