有時候我們希望透過 PHP 直接輸出一個 .url 檔案,讓使用者點擊後能自動開啟指定網址。這在建立「加入我的最愛」或「桌面捷徑」功能時非常實用。本文示範如何設定正確的 HTTP 標頭並輸出符合 InternetShortcut 格式的內容。
<?php
/**
* Internet Shortcut 產生器
*
* 建議在 HTML <head> 區塊加入 favicon 連結:
* <link href="favicon.ico" rel="icon" type="image/x-icon" />
* <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
* <link href="favicon.ico" rel="bookmark" type="image/x-icon" />
*/
$shortcut = '[InternetShortcut]
URL=https://onens.com/
IDList=
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
';
// 設定回應標頭:強制下載、指定檔名
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=Onens.Com.url');
// 輸出捷徑內容
echo $shortcut;
?>
application/octet-stream 告訴瀏覽器這是二進位檔案,觸發下載行為。attachment; filename=... 指定下載時的預設檔名(副檔名必須為 .url)。[InternetShortcut] 區塊為 Windows 捷徑標準格式,URL 欄位即為目標網址。可整合至會員中心「建立桌面捷徑」按鈕、行銷活動「一鍵加入收藏」等功能,提升使用者留存率。