利用 HTTP Header 強制瀏覽器下載 .url 文件(Internet Shortcut 格式),用戶雙擊即可在默認瀏覽器打開指定網址。
<?php
$url = $_GET['url'] ?? 'https://www.baidu.com';
$filename = $_GET['name'] ?? 'shortcut';
$filename = iconv('UTF-8', 'GBK', $filename); // 中文文件名編碼轉換
$shortcut = "[InternetShortcut]\r\n";
$shortcut .= "URL=$url\r\n";
$shortcut .= "IDList=\r\n";
$shortcut .= "[{000214A0-0000-0000-C000-000000000046}]\r\n";
$shortcut .= "Prop3=19,2\r\n";
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"{$filename}.url\"");
echo $shortcut;
?>
在頁面放置鏈接:
<a href="shortcut.php?url=https://example.com&name=我的網站">下載桌面快捷方式</a>
shortcut.php 文件調用。iconv('UTF-8', 'GBK', ...) 轉碼,避免下載文件名亂碼。IDList 和 Prop3 為固定 GUID 與屬性,保證 Windows 識別為快捷方式。JS 僅能通過 ActiveXObject('WScript.Shell') 創建快捷方式,**僅 IE 支持**。現代瀏覽器無法直接操作文件系統,需後端生成文件供下載。