將介紹使用php生成網頁桌面快捷方式的代碼,并添加圖標及解決不同瀏覽器保存出現的亂碼問題。
我們訪問網站時,如果網站的內容很有吸引,一般我們都會使用瀏覽器的收藏夾功能,收藏此網站。
在瀏覽器收藏的網頁,需要打開瀏覽器,再從收藏夾選定訪問。
如果可以在桌面直接進入到網站,這樣可以為用戶訪問提供便利。
我們可以使用php創建網頁的快捷入口文件,保存到用戶桌面,方便用戶快速訪問。
生成代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?php $filename = '破曉領域.url' ; $url = 'http://fdipzone.com/' ; $icon = 'http://fdipzone.com/favicon.ico' ; createShortCut( $filename , $url , $icon ); /** * 創建保存為桌面代碼 * @param String $filename 保存的文件名 * @param String $url 訪問的連接 * @param String $icon 圖標路徑 */ function createShortCut( $filename , $url , $icon = '' ){ // 創建基本代碼 $shortCut = "[InternetShortcut]\r\nIDList=[{000214A0-0000-0000-C000-000000000046}]\r\nProp3=19,2\r\n" ; $shortCut .= "URL=" . $url . "\r\n" ; if ( $icon ){ $shortCut .= "IconFile=" . $icon . "" ; } header( "content-type:application/octet-stream" ); // 獲取用戶瀏覽器 $user_agent = $_SERVER [ 'HTTP_USER_AGENT' ]; $encode_filename = rawurlencode( $filename ); // 不同瀏覽器使用不同編碼輸出 if (preg_match( "/MSIE/" , $user_agent )){ header( 'content-disposition:attachment; filename="' . $encode_filename . '"' ); } else if (preg_match( "/Firefox/" , $user_agent )){ header( "content-disposition:attachment; filename*=\"utf8''" . $filename . '"' ); } else { header( 'content-disposition:attachment; filename="' . $filename . '"' ); } echo $shortCut ; } ?> |
下載保存到桌面
保存到桌面
在桌面保存為*.url后,點擊就能自動打開瀏覽器并訪問網站內容了。
第二種情況:PHP實現網站保存快捷桌面方式
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php /* 保存shortcut.php訪問即可保存桌面 */ $title = "服務器之家" ; $Shortcut = "[InternetShortcut] URL=https: //www.ythuaji.com.cn IDList= <br>[{000214A0-0000-0000-C000-000000000046}] Prop3=19,2"; Header( "Content-type: application/octet-stream" ); header( "Content-Disposition: attachment; filename=" . $title . ".url;" ); echo $Shortcut ; ?> |
第三種情況:PHP生成網站桌面快捷方式
PHP生成桌面快捷方式就是這么的簡單,大家生成的時候改下你要生成的網站即可。
dianji.html代碼:
<a href="a.php?url=www.ythuaji.com.cn&name=服務器之家">生成左面快捷方式</a>
shengcheng.php代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php //網站生存左面快捷方式---功能 $url = $_GET [ 'url' ]; $filename = urldecode( $_GET [ 'name' ]); $filename = iconv( 'GBk' , 'utf-8' , $filename ); //字符集轉換(沒有需要轉的就不轉) if (! $url || ! $filename ) exit (); $Shortcut = "[InternetShortcut] URL={ $url } IDList= [{000214A0-0000-0000-C000-000000000046}] Prop3=19,2"; header( "Content-type: application/octet-stream" ); header( "Content-Disposition: attachment; filename={$filename}.url;" ); echo $Shortcut ; ?> |
希望本文所述對大家學習php程序設計有所幫助。