通過 PHP 動態生成 Windows 桌面快捷方式(.url 文件),讓用戶點擊鏈接即可下載包含指定網址的快捷方式,雙擊即可在默認瀏覽器中打開對應網頁。這對於推廣網站、提供快速訪問入口非常實用。
<?php
/**
* 生成並下載 .url 桌面快捷方式
* @param string $targetUrl 目標網址
* @param string $shortcutName 快捷方式名稱(不含副檔名)
* @param string $iconUrl 圖標 URL(可選)
*/
function downloadUrlShortcut($targetUrl, $shortcutName = 'Website', $iconUrl = '') {
// 驗證 URL 格式
if (!filter_var($targetUrl, FILTER_VALIDATE_URL)) {
http_response_code(400);
exit('無效的目標網址');
}
// 構建 .url 文件內容(INI 格式)
$content = "[InternetShortcut]\r\n";
$content .= "URL={$targetUrl}\r\n";
$content .= "IDList=\r\n";
// 設置圖標(優先使用指定圖標,其次嘗試 favicon.ico)
if ($iconUrl) {
$content .= "IconFile={$iconUrl}\r\n";
} elseif (parse_url($targetUrl, PHP_URL_HOST)) {
$scheme = parse_url($targetUrl, PHP_URL_SCHEME) ?: 'https';
$host = parse_url($targetUrl, PHP_URL_HOST);
$content .= "IconFile={$scheme}://{$host}/favicon.ico\r\n";
}
$content .= "IconIndex=1\r\n";
// 標準 CLSID(固定值)
$content .= "[{000214A0-0000-0000-C000-000000000046}]\r\n";
$content .= "Prop3=19,2\r\n";
// 設置響應頭
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . rawurlencode($shortcutName) . '.url"');
header('Content-Length: ' . strlen($content));
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
echo $content;
exit;
}
// 使用示例
// 直接訪問:shortcut.php?url=https://example.com&name=MySite&icon=https://example.com/favicon.ico
if (isset($_GET['url'])) {
$name = $_GET['name'] ?? 'Website';
$icon = $_GET['icon'] ?? '';
downloadUrlShortcut($_GET['url'], $name, $icon);
}
?>
.url 文件本質是 Windows 系統識別的 INI 格式文本文件,核心字段說明:
<?php
/**
* 批量生成快捷方式打包下載(ZIP)
*/
function downloadMultipleShortcuts(array $sites, $zipName = 'shortcuts.zip') {
// 需啟用 ZipArchive 擴展
$zip = new ZipArchive();
$tempDir = sys_get_temp_dir() . '/shortcuts_' . uniqid();
mkdir($tempDir, 0755, true);
foreach ($sites as $site) {
$name = $site['name'] ?? 'Website';
$url = $site['url'] ?? '';
$icon = $site['icon'] ?? '';
if (!$url) continue;
$content = "[InternetShortcut]\r\nURL={$url}\r\n";
if ($icon) $content .= "IconFile={$icon}\r\n";
$content .= "IconIndex=1\r\n[{000214A0-0000-0000-C000-000000000046}]\r\nProp3=19,2\r\n";
$safeName = preg_replace('/[\\\\\/:*?"<>|]/', '_', $name);
file_put_contents("{$tempDir}/{$safeName}.url", $content);
}
if ($zip->open($zipName, ZipArchive::CREATE) === TRUE) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempDir));
foreach ($files as $file) {
if (!$file->isDir()) {
$zip->addFile($file->getRealPath(), $file->getFilename());
}
}
$zip->close();
}
// 清理臨時文件並輸出 ZIP
// ... (省略清理和輸出代碼)
}
// 企業級應用:生成帶有追蹤參數的快捷方式
function generateTrackedShortcut($baseUrl, $campaignId, $channel) {
$trackedUrl = $baseUrl . '?utm_source=shortcut&utm_medium=desktop&utm_campaign=' . $campaignId . '&utm_content=' . $channel;
return downloadUrlShortcut($trackedUrl, "Campaign_{$campaignId}_{$channel}");
}
?>
| 瀏覽器 | 行為 | 備註 |
|---|---|---|
| Chrome/Edge/Firefox | 直接下載 .url 文件 | 雙擊即可在默認瀏覽器打開 |
| Safari (macOS) | 下載 .url 文件 | macOS 不原生支持 .url,需第三方工具 |
| 移動端瀏覽器 | 可能嘗試直接打開而非下載 | 建議移動端隱藏此功能 |
javascript:、data:)這項功能雖然簡單,但對於營銷推廣、客戶服務、內部工具分發等場景極具價值,只需幾十行代碼即可實現專業級的快捷方式生成服務。