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

ASP 內建元件深度解析:瀏覽器偵測、檔案存取、廣告輪播與內容連結

     編輯:PHP 綜合知識

前言

ASP(Active Server Pages)透過內建 COM 元件擴展伺服器功能。本文詳細介紹四大核心元件:Browser Capabilities(瀏覽器能力偵測)、FileSystemObject(檔案系統操作)、Ad Rotator(廣告輪播)與 Content Linking(內容連結導航),並提供完整範例程式碼與實務建議。

一、Browser Capabilities Component(瀏覽器能力偵測)

根據 Browscap.ini 資料庫解析 User-Agent,回傳瀏覽器支援特性(框架、ActiveX、JavaScript、Cookies 等)。

使用步驟

  1. 確保伺服器 %windir%\system32\inetsrv\browscap.ini 為最新版(可從 browscap.org 下載)。
  2. 使用 Server.CreateObject("MSWC.BrowserType") 建立物件。
  3. 讀取屬性:Browser、Version、Frames、JavaScript、Cookies 等。

完整範例

<%@ Language=VBScript %>
<% Option Explicit %>
<html><body>
<%
Dim bc
Set bc = Server.CreateObject("MSWC.BrowserType")
%>
<table border="1" cellpadding="5">
<tr><th>屬性</th><th>值</th></tr>
<tr><td>瀏覽器</td><td><%= bc.Browser %></td></tr>
<tr><td>版本</td><td><%= bc.Version %></td></tr>
<tr><td>支援表格</td><td><%= bc.Tables %></td></tr>
<tr><td>支援 ActiveX</td><td><%= bc.ActiveXControls %></td></tr>
<tr><td>支援 Java Applets</td><td><%= bc.JavaApplets %></td></tr>
<tr><td>支援 JavaScript</td><td><%= bc.JavaScript %></td></tr>
<tr><td>支援 Cookies</td><td><%= bc.Cookies %></td></tr>
<tr><td>支援框架</td><td><%= bc.Frames %></td></tr>
<tr><td>作業系統</td><td><%= bc.Platform %></td></tr>
<tr><td>支援 VBScript</td><td><%= bc.VBScript %></td></tr>
</table>
<%
Set bc = Nothing ' 釋放資源
%>
</body></html>

實務應用

  • 針對不支援 CSS Grid 的舊版 IE 提供浮動版面備案。
  • 偵測行動裝置導向 RWD 版面或獨立行動站。
  • 記錄瀏覽器統計分析。

二、File Access Component(檔案系統操作)

由 FileSystemObject(目錄/檔案管理)與 TextStream(讀寫文字檔)組成,提供完整的檔案系統存取能力。

核心物件與方法

物件常用方法/屬性
FileSystemObjectCreateFolder、DeleteFolder、FolderExists、CreateTextFile、OpenTextFile、FileExists、GetFile、GetFolder、GetDrive
TextStreamReadLine、ReadAll、Write、WriteLine、Close、AtEndOfStream

完整範例:建立目錄、寫入/讀取文字檔

<%@ Language=VBScript %>
<% Option Explicit %>
<%
Dim fso, folderPath, filePath, ts
Set fso = Server.CreateObject("Scripting.FileSystemObject")

' 取得虛擬路徑對應的實體路徑
folderPath = Server.MapPath("data")
filePath = folderPath & "\log.txt"

' 建立目錄(若不存在)
If Not fso.FolderExists(folderPath) Then fso.CreateFolder(folderPath)

' 寫入模式:8=Append, 2=Overwrite, 1=ReadOnly
Set ts = fso.OpenTextFile(filePath, 8, True, 0) ' 0=ASCII, -1=Unicode
ts.WriteLine "[" & Now & "] 使用者造訪"
ts.Close

' 讀取並顯示
Set ts = fso.OpenTextFile(filePath, 1, False, 0)
Do While Not ts.AtEndOfStream
    Response.Write Server.HTMLEncode(ts.ReadLine) & "<br>"
Loop
ts.Close

Set ts = Nothing
Set fso = Nothing
%>

進階應用:網頁計數器

<%
Function GetHitCount(counterFile)
    Dim fso, ts, count
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(counterFile) Then
        Set ts = fso.CreateTextFile(counterFile, True)
        ts.Write "0"
        ts.Close
    End If
    Set ts = fso.OpenTextFile(counterFile, 2, False, 0) ' Overwrite
    count = CLng(ts.ReadAll) + 1
    ts.Write count
    ts.Close
    GetHitCount = count
    Set fso = Nothing
End Function

Response.Write "總造訪次數:" & GetHitCount(Server.MapPath("counter.txt"))
%>

三、Ad Rotator Component(廣告輪播)

根據設定檔(紅向文字檔)隨機顯示圖片廣告,支援點擊追蹤、權重分配、框架目標。

設定檔格式 adrot.txt

REDIRECT /click.asp       ' 點擊導向處理頁
WIDTH 468
HEIGHT 60
BORDER 1
*
https://example.com/ad1.gif
https://example.com/product1
產品一優惠
50
https://example.com/ad2.gif
https://example.com/product2
新品上市
30
https://example.com/ad3.gif
-
品牌形象廣告
20

每組廣告四行:圖片 URL、連結 URL(- 表無連結)、替代文字、權重(相對機率)。

顯示頁面

<%@ Language=VBScript %>
<% Option Explicit %>
<%
Dim ad
Set ad = Server.CreateObject("MSWC.AdRotator")
ad.Border = 1
ad.Clickable = True
ad.TargetFrame = "_blank"
Response.Write ad.GetAdvertisement(Server.MapPath("adrot.txt"))
Set ad = Nothing
%>

點擊導向頁 click.asp

<%@ Language=VBScript %>
<% Option Explicit %>
<%
Dim url
url = Request.QueryString("url")
If url <> "" Then Response.Redirect url
%>

四、Content Linking Component(內容連結導航)

自動生成「上一頁/下一頁」或「章節目錄」導航,依據文字檔定義的順序動態產生連結,免除手動維護。

定義檔 toc.txt

chap1.asp	第一章:ASP 入門
chap2.asp	第二章:內建物件
chap3.asp	第三章:元件應用
chap4.asp	第四章:資料庫連接
chap5.asp	第五章:專案實戰

以 Tab 鍵分隔 URL 與標題。

目錄頁 index.asp

<%@ Language=VBScript %>
<% Option Explicit %>
<html><body>
<h2>教學目錄</h2>
<ul>
<%
Dim nl, count, i, url, desc
Set nl = Server.CreateObject("MSWC.NextLink")
count = nl.GetListCount(Server.MapPath("toc.txt"))
For i = 1 To count
    url = nl.GetNthURL(Server.MapPath("toc.txt"), i)
    desc = nl.GetNthDescription(Server.MapPath("toc.txt"), i)
    Response.Write "
  • " & desc & "
  • " & vbCrLf Next Set nl = Nothing %> </ul> </body></html>

    內容頁通用導航 nav.inc(以 引入)

    <%
    Dim nl, idx, total
    Set nl = Server.CreateObject("MSWC.NextLink")
    idx = nl.GetListIndex(Server.MapPath("toc.txt"))
    total = nl.GetListCount(Server.MapPath("toc.txt"))
    
    Response.Write "
    " If idx > 1 Then Response.Write "| 上一章 |" End If If idx < total Then Response.Write "| 下一章 |" End If Set nl = Nothing %>

    五、效能與安全建議

    • 釋放物件:使用完畢務必 Set obj = Nothing,避免記憶體洩漏。
    • 快取 Browscap:Browscap.ini 載入有成本,高流量站點建議快取偵測結果或改用 WURFL/DeviceAtlas。
    • 檔案權限:FileSystemObject 需要 IUSR/Application Pool 身分具備目錄讀寫權限,嚴格限制可寫入目錄範圍。
    • 廣告檔案驗證:定期檢查廣告圖片是否存在、連結是否有效,避免死連結影響體驗。
    • 編碼一致性:所有文字檔(adrot.txt, toc.txt)儲存為 UTF-8 with BOM 或 ANSI,避免中文亂碼。

    結語

    ASP 內建元件雖屬經典技術,但在維護舊系統或特定 Windows 伺服器環境時仍具實用價值。掌握瀏覽器偵測、檔案操作、廣告輪播與內容導航四大元件,能快速實作常見網站功能。現代開發建議遷移至 ASP.NET Core 或跨平台框架,但了解經典 ASP 架構有助於系統重構與遷移規劃。

    1. 上一篇文章:
    2. 下一篇文章:
    Copyright © 程式碼之家 All Rights Reserved