首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 生成桌面快捷方式 (.url) 讓用戶一鍵收藏網站

     編輯:PHP 相關知識

原理

Windows 桌面快捷方式本質是 .url 文本文件,格式為 INI 風格。PHP 動態生成並以 application/octet-stream 下發,瀏覽器彈出「保存為」對話框,用戶保存到桌面即可一鍵訪問。

生成腳本 (shortcut.php)

<?php
// 配置
$siteUrl = 'https://www.example.com/';
$shortcutName = '我的網站'; // 保存的文件名 (不含 .url)

// 構建 .url 內容
$content = <<

頁面調用鏈接

<a href="shortcut.php" class="btn btn-primary">
    <i class="bi bi-desktop"></i> 發送到桌面
</a>

進階:動態生成當前頁面快捷方式

<?php
// current_page_shortcut.php
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$currentUrl = "{$protocol}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$pageTitle = '當前頁面'; // 可從  獲取或傳參

$content = "[InternetShortcut]\nURL={$currentUrl}\nIDList=\n[{000214A0-0000-0000-C000-000000000046}]\nProp3=19,2\n";

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . rawurlencode($pageTitle) . '.url"');
echo $content;
?></code></pre>

<h2>圖標設置 (Favicon)</h2>
<ol>
<li>在網站根目錄放置 <code>favicon.ico</code> (16x16, 32x32, 48x48 多尺寸)</li>
<li>HTML 中聲明:<code><link rel="icon" href="/favicon.ico"></code></li>
<li>Windows 會自動從網站獲取 Favicon 作為快捷方式圖標</li>
<li>若需強制指定圖標 URL,可在 .url 中添加:<code>IconFile=https://example.com/favicon.ico</code><br><code>IconIndex=0</code></li>
</ol>

<h2>跨平台兼容性說明</h2>
<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse; width: 100%;">
<thead><tr style="background:#f0f0f0;"><th>平台</th><th>支持 .url</th><th>替代方案</th></tr></thead>
<tbody>
<tr><td>Windows</td><td>✅ 原生支持</td><td>—</td></tr>
<tr><td>macOS</td><td>❌</td><td>生成 <code>.webloc</code> (XML 格式) 或引導用戶拖拽網址到 Dock</td></tr>
<tr><td>Linux (GNOME/KDE)</td><td>❌</td><td>生成 <code>.desktop</code> 文件 (<code>Type=Link</code>, <code>URL=...</code>)</td></tr>
<tr><td>移動端</td><td>❌</td><td>引導「添加到主屏幕」(PWA Manifest) 或分享→添加到主屏幕</td></tr>
</tbody>
</table>

<h2>完整跨平台生成器 (示例)</h2>
<pre><code><?php
function generateShortcut(string $url, string $title): array {
    $win = "[InternetShortcut]\nURL={$url}\nIconFile={$url}favicon.ico\nIconIndex=0\n";
    $mac = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"https://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n<key>URL</key><string>{$url}</string>\n</dict>\n</plist>";
    $linux = "[Desktop Entry]\nType=Link\nURL={$url}\nName={$title}\nIcon={$url}favicon.ico\n";

    return [
        'windows' => ['content'=>$win, 'ext'=>'url', 'mime'=>'application/octet-stream'],
        'macos'   => ['content'=>$mac, 'ext'=>'webloc', 'mime'=>'application/x-plist'],
        'linux'   => ['content'=>$linux, 'ext'=>'desktop', 'mime'=>'application/x-desktop'],
    ];
}

// 使用
$shortcuts = generateShortcut('https://example.com/', 'Example Site');
// 前端提供下載鏈接或按鈕
?></code></pre>

<h2>安全提示</h2>
<ul>
<li>僅在受信任頁面提供下載,防止釣魚網站偽裝生成惡意快捷方式</li>
<li>驗證 Referer 或 CSRF Token,防止 CSRF 攻擊誘導用戶下載</li>
<li>文件名使用 <code>rawurlencode()</code> 編碼,防止中文亂碼或注入</li>
</ul>
							<div class="qcxs_ie_he"></div>
						</div>
						<div class="akevx_own"> <script language='javascript' src='/ad/p.js'></script></div>
						<div class="akevx_mj"> </div>	
						<div class="qcxs_ie_he"></div>
						
						<ol class="qmsbvx_hsb ovv_hex">
							<li>上一篇文章: <a class='LinkPrevArticle' href='https://code.yunyiwangluo.com/bianchen/gyphp/42567.html' >PHP echo 輸出逗號 vs 點號:性能差異、語法區別與最佳實踐</a> <li>下一篇文章: <a class='LinkNextArticle' href='https://code.yunyiwangluo.com/bianchen/gyphp/42569.html'  >PHP 圖片輸出亂碼問題:正確分離輸出與 HTML 的做法</a> 
						</ol>
					</div>
				</div>
				<div class="qirnxbd bg1 mt20">
					<div class="jdbsbav">
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/' target="_blank">PHP 相關知識</a>
						<div class="jdbsbav_hx"></div>
					</div>
					<div class="qirnxbd_rbx">
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/137670.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011615583760.png'  width='100' height='70' alt='Smarty 模板引擎基礎搭建完整步驟指南'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/137670.html' target="_blank"><strong>Smarty 模板引擎基礎搭建完整步驟指南</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/137670.html' target="_blank"><p>Smarty 模板引擎基礎搭建完整步驟指南
Smarty 是</p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/146029.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011711203722_S.jpg'  width='100' height='70' alt='PHP 神盾解密工具:自動化還原加密腳本原始碼'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/146029.html' target="_blank"><strong>PHP 神盾解密工具:自動化還原加密腳本原始碼</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/146029.html' target="_blank"><p>基於前文解密原理分析,整理發布可用的自動化解密工具。

工具</p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/155701.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011717514681.png'  width='100' height='70' alt='微信公眾平台開發(一):接口配置全流程——BAE/SAE 部署與 Token 驗證'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/155701.html' target="_blank"><strong>微信公眾平台開發(一):接口配置全流程——BAE/SAE 部署與 Token 驗證</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/155701.html' target="_blank"><p>從零搭建微信公眾號後端:註冊、雲部署、接口驗證完整教程

1</p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/136203.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011614190864.png'  width='100' height='70' alt='MongoDB 主從復制 (Master-Slave) 架構原理與配置'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/136203.html' target="_blank"><strong>MongoDB 主從復制 (Master-Slave) 架構原理與配置</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/136203.html' target="_blank"><p>MongoDB 主從復制 (Master-Slave) 架構</p></a>			
						</div>
						
						<div class="qcxs_ie_he"></div>
					</div>
				</div>
			</div>
			<div class="eccsgxsg mt20">
				
				<div class="akevx_oin"> </div>	
				<div class="nenxnxb bg1 mt20">
					<div class="enxvsfgg">
						<div class="enxvsfgg_1"></div>
					</div>
					<div class="pdirbvx lbhevs ovv_hex">
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/41926.html' target="_blank">PHP 循序查找與二分查找實作比較:演算法原理、程式碼與效能分析</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/159393.html' target="_blank"><xTitle>PHP xmlhttp 亂碼問題徹底解決:編碼鏈路完整配置指南</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/142844.html' target="_blank">74CMS 騎士人才系統 v3.5.1 正式版發布:修復多項 Bug 與安全增強</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/154535.html' target="_blank">會員收費系統開發文檔 v1.0:四大展示模式與價格策略設計</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/141389.html' target="_blank">PHP 實作事件機制:基於 debug_backtrace 的觀察者模式進階應用</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/140007.html' target="_blank">Yii 模板中常用變量與方法速查表:URL 構建、路徑獲取、請求判斷、小組件渲染</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/151317.html' target="_blank">PHPMailer 核心屬性與方法完整中文參考手冊</a>
						
					</div>
				</div>
			
				<div class="eqibneb bg1 mt20">
					<div class="lkebxsg">欄目導航</div>
					<div class="eqibneb_1 ovv_hex">
						<li><a class='childclass' href='https://code.yunyiwangluo.com/bianchen/phpjc/' target="_self">PHP 基礎</a></li>
<li><a class='childclass' href='https://code.yunyiwangluo.com/bianchen/phpzh/' target="_self">PHP 綜合知識</a></li>
<li><a class='childclass' href='https://code.yunyiwangluo.com/bianchen/phprm/' target="_self">PHP 入門</a></li>
<li><a class='childclass' href='https://code.yunyiwangluo.com/bianchen/gyphp/' target="_self">PHP 相關知識</a></li>
					</div>
					
				</div>
			</div>
			<div class="qcxs_ie_he"></div>
		</div>
	</div>
		<div class="qcxs_ie_he"></div>
	<div class="ebrxbsbf">Copyright © <a href="https://code.yunyiwangluo.com/" target="_blank" title="程式碼之家">程式碼之家</a> All Rights Reserved   </div>


</body>
</html>