從http://www.oioj.net/上下載了最新的oblog2.52測(cè)試版,安裝并試用,感覺(jué)作者葉開(kāi)程序?qū)懙暮芎?,故打算將代碼從頭到尾看一遍,學(xué)習(xí)一下??吹倪^(guò)程肯定有所收獲,因此記錄下來(lái)以備后查。 要分析網(wǎng)頁(yè)或ASP程序應(yīng)該從網(wǎng)站的首頁(yè)開(kāi)始,一般是index.htm、index.asp、default.htm、 default.asp等(當(dāng)然了網(wǎng)站的首頁(yè)是可以自己定義的,象IIS中就可以定義網(wǎng)站的起始頁(yè)面), 就像在C語(yǔ)言中分析一個(gè)函數(shù)要從main函數(shù)入手一樣。 首頁(yè)當(dāng)然是顯示第一個(gè)頁(yè)面了(廢話?),不過(guò)在本系統(tǒng)中,該頁(yè)面顯示的有點(diǎn)麻煩(我自己 這樣認(rèn)為的),要經(jīng)過(guò)很多轉(zhuǎn)換,判斷很多東西,最后顯示出來(lái),不過(guò)正式的軟件是應(yīng)該這樣 做的。 <!--#include file="conn.asp"--> <!--#include file="inc/bloginfo.asp"--> <!--#include file="inc/syscode.asp"--> <!--#include file="inc/function.asp"--> 以上4行是包含文件,他們的功能就相當(dāng)于C語(yǔ)言中的頭文件一樣。#include指令將指定文件的 內(nèi)容在ASP服務(wù)器執(zhí)行之前插入到ASP文件中。這些指定的文件一般都是一些在多個(gè)頁(yè)面內(nèi)都要 用到的一些公共函數(shù)、變量等網(wǎng)頁(yè)元素。 <% dim show dim 用來(lái)定義變量,屬于VB語(yǔ)法格式 Application對(duì)象用來(lái)在應(yīng)用中共享信息。應(yīng)用包括在指定的虛擬目錄下及其子目錄下的所有 asp文件。cachename是一個(gè)常量在conn.asp文件中,其值為"oblog"。 if Application(cachename&"index_update")=false and application(cachename&"index")<>"" then ‘ 判斷首頁(yè)文件是否已經(jīng)生成并保存,通過(guò)這種機(jī)制可以實(shí)現(xiàn)記住使用者 show=application(cachename&"index") else 如果沒(méi)有則重新生成。重新產(chǎn)生index文件時(shí),先從sysskin表中查找系統(tǒng)設(shè)定的缺省值。 dim rstmp set rstmp=conn.execute("select skinmain from sysskin where isdefault=‘true‘") show=rstmp(0) set rstmp=nothing call indexshow() indexshow函數(shù)在文件syscode.asp中,用來(lái)產(chǎn)生index文件。 Application.Lock application(cachename&"index")=show Application(cachename&"index_update")=false Application.unLock if Application(cachename&"siterefu")<>"" then conn.execute("update [bloginfo] set siterefu_num="&Application(cachename&"siterefu")) end if end if if instr(show,"$show_userlogin$")>0 then call showuserlogin() show=replace(show,"$show_userlogin$",show_userlogin) end if response.Write show 最終將頁(yè)面顯示出來(lái) call bottom() %>
數(shù)據(jù)庫(kù)連接
目前大多數(shù)網(wǎng)站都用到了數(shù)據(jù)庫(kù),不論是大型的數(shù)據(jù)庫(kù)如Oracle、SQL Server還小型的象Access、MySQL。數(shù)據(jù)庫(kù)可以將用戶(hù)的資料和網(wǎng)站的信息按照一定的形式存儲(chǔ)起來(lái),方便信息的檢索、組織。 用ASP來(lái)進(jìn)行網(wǎng)站開(kāi)發(fā)的時(shí)候一般是用ADO來(lái)進(jìn)行數(shù)據(jù)庫(kù)的操作。ADO(ActiveX Data Object)是允許用戶(hù)與數(shù)據(jù)存儲(chǔ)進(jìn)行交互的組件,它能使客戶(hù)端應(yīng)用程序通過(guò)OLE DB提供者訪問(wèn)和操作數(shù)據(jù)庫(kù)服務(wù)器中的數(shù)據(jù)。常用的ADO對(duì)象主要有下面幾個(gè): Connection 連接數(shù)據(jù)庫(kù) Command 對(duì)數(shù)據(jù)庫(kù)執(zhí)行命令 Recordset 執(zhí)行命令返回所包含的數(shù)據(jù)集 在oblog中,文件conn.asp包含的就是實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接的代碼。 首先在代碼的開(kāi)始用Option Explicit語(yǔ)句強(qiáng)制要求顯式聲明所有變量,即所有的變量都必須用Dim、Public、Private和Redim語(yǔ)句顯式聲明,如果試圖使用未經(jīng)聲明的變量名,則會(huì)出現(xiàn)錯(cuò)誤,這是一種良好的編程風(fēng)格。接下來(lái)進(jìn)行數(shù)據(jù)庫(kù)連接。 const issqldate=false if not issqldate then Dim Db Db = "data/oblog2.mdb" ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(db) else ‘sql數(shù)據(jù)庫(kù)連接參數(shù):數(shù)據(jù)庫(kù)名、用戶(hù)密碼、用戶(hù)名、連接名(本地用local,外地用IP) Dim SqlDatabaseName,SqlPassword,SqlUsername,SqlLocalName SqlDatabaseName = "oblog" SqlPassword = "oblog" SqlUsername = "oblog" SqlLocalName = "(local)" ConnStr = "Provider = Sqloledb; User ID = " & SqlUsername & "; Password = " & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source = " & SqlLocalName & ";" end if ‘On Error Resume Next Set conn = Server.CreateObject("ADODB.Connection") conn.open ConnStr If Err Then err.Clear Set Conn = Nothing Response.Write "數(shù)據(jù)庫(kù)連接出錯(cuò),請(qǐng)檢查連接字串。" Response.End End If
在這里作者使用了一個(gè)變量issqldate以判斷數(shù)據(jù)庫(kù)是用Access還是SQL Server,該版本代碼中使用的是Access數(shù)據(jù)庫(kù)。先定義數(shù)據(jù)庫(kù)連接字符串ConnStr,然后創(chuàng)建一個(gè)ADODB.Connection對(duì)象,最后用Connection的Open方法打開(kāi)數(shù)據(jù)庫(kù)。 判斷是否有錯(cuò)誤出現(xiàn),當(dāng)出現(xiàn)錯(cuò)誤時(shí),先清空Connection,然后用Response.End結(jié)束頁(yè)面的處理。 下面代碼用來(lái)實(shí)現(xiàn)系統(tǒng)信息的自動(dòng)存取。 const cachename="oblog" ‘緩存對(duì)象前綴名 if isarray(application(cachename&"info"))=false then call ***pplicationValue() end if ‘call ***pplicationValue() sub closeconn() conn.close set conn=nothing end sub sub ***pplicationValue() dim infosql,rsinfo infosql="select show_log_num,enguestcomment,site_keyword,needclassid,sitefriends," infosql=infosql&"blogcheck,copyrighturl,siteurl,siterefu_num,sitetitle,webmasteremail," infosql=infosql&"logcount,getlog_num,EnableUserReg,logcount,commentcount,messagecount,usercount,timediff," infosql=infosql&"sitename,show_imgw_num,show_img_mouse,blog_showruntime,blog_showrefu,vip_prosee,show_listuser_num," infosql=infosql&"blog_profilt,show_comment_asc,userlink_add,user_sendword,siteplacard,smalledit,gourl" infosql=infosql&" from bloginfo" set rsinfo=conn.execute(infosql) Redim ApplicationValue(34) dim i for i=0 to 32 ApplicationValue(i)=rsinfo(i) next set rsinfo=nothing Application.Lock set Application(cachename&"info")=nothing Application(cachename&"info")=ApplicationValue Application(cachename&"index_update")=true Application(cachename&"list_update")=true Application.unlock end sub 上面代碼中還提供了一個(gè)函數(shù)closeconn(),用來(lái)關(guān)閉數(shù)據(jù)庫(kù)。我個(gè)人感覺(jué)conn.asp中所實(shí)現(xiàn)的代碼有點(diǎn)亂,應(yīng)該有更加結(jié)構(gòu)化、更清晰的實(shí)現(xiàn)方法(比如數(shù)據(jù)庫(kù)的打開(kāi)是否可以用函數(shù)來(lái)表示),具體怎么實(shí)現(xiàn)我也沒(méi)有想好,在此先記錄下來(lái)。 |