PHP生成网页快捷方式代码实现
首先创建一个PHP文件,文件名可以随意设定,例如命名为shortcut.php以便记忆。
在该PHP文件中编写以下核心逻辑:
具体代码如下所示:
详细代码实现如下:
<?php
$Shortcut = "[InternetShortcut]
URL=https://www.jb51.net/
IDList=
[{000214A0-0000-0000-C000-000000000046}]"
Prop3=19,2";
";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=本站.url;");
echo $Shortcut;
?> 若希望生成的快捷方式包含网站图标,请确保网站根目录下存在favicon.ico文件。
将上述代码上传至服务器后,通过浏览器访问类似https://www.jb51.net/shortcut.php的地址,系统将提示下载一个名为“本站.url”的文件,保存至本地后即可作为网页快捷方式使用。
以下是使用ASP实现的等效代码:
ASP代码示例如下:
<%
id=int(request("id"))
if id="" then id="1"
title=request("title")
If title="" Then title="本站"
Shortcut = "[InternetShortcut] " & vbCrLf
Shortcut = Shortcut & "URL=https://www.jb51.net/article/"&id&".htm?desktop" & vbCrLf
Shortcut = Shortcut & "IDList= " & vbCrLf
Shortcut = Shortcut & "[{000214A0-0000-0000-C000-000000000046}] " & vbCrLf
Shortcut = Shortcut & "Prop3=19,2 " & vbCrLf
Shortcut = Shortcut & " " & vbCrLf
Response.AddHeader "Content-Disposition", "attachment;filename="&title&".url;"
Response.ContentType = "application/octet-stream"
Response.Write Shortcut
%>
需要注意的是,出于安全考虑,部分系统可能会锁定此类文件。
若要正常使用,需右键点击“本站.url”文件,选择属性,在属性对话框中点击“解除锁定”按钮,此操作类似于处理从网络下载的.chm文件。
