| 分类: ASP |
作者:岂有此李
从http://www.oioj.net/上下载了最新的oblog2.52测试版,安装并试用,感觉作者叶开程序写的很好,故打算将代码从头到尾看一遍,学习一下。看的过程肯定有所收获,因此记录下来以备后查。
要分析网页或ASP程序应该从网站的首页开始,一般是index.htm、index.asp、default.htm、default.asp等(当然了网站的首页是可以自己定义的,象IIS中就可以定义网站的起始页面),就像在C语言中分析一个函数要从main函数入手一样。首页当然是显示第一个页面了(废话?),不过在本系统中,该页面显示的有点麻烦(我自己这样认为的),要经过很多转换,判断很多东西,最后显示出来,不过正式的软件是应该这样
做的。
<!--#include file="conn.asp"-->
<!--#include file="inc/bloginfo.asp"-->
<!--#include file="inc/syscode.asp"-->
<!--#include file="inc/function.asp"-->
以上4行是包含文件,他们的功能就相当于C语言中的头文件一样。#include指令将指定文件的内容在ASP服务器执行之前插入到ASP文件中。这些指定的文件一般都是一些在多个页面内都要用到的一些公共函数、变量等网页元素。
<%
dim show
dim 用来定义变量,属于VB语法格式
Application对象用来在应用中共享信息。应用包括在指定的虚拟目录下及其子目录下的所有
asp文件。cachename是一个常量在conn.asp文件中,其值为"oblog"。
if Application(cachename&"index_update")=false and application(cachename&"index")<>"" then
'
判断首页文件是否已经生成并保存,通过这种机制可以实现记住使用者
show=application(cachename&"index")
else
如果没有则重新生成。重新产生index文件时,先从sysskin表中查找系统设定的缺省值。
dim rstmp
set rstmp=conn.execute("select skinmain from
sysskin where isdefault='true'")
show=rstmp(0)
set rstmp=nothing
call indexshow()
indexshow函数在文件syscode.asp中,用来产生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
最终将页面显示出来
call bottom()
%>
做的。
<!--#include file="conn.asp"-->
<!--#include file="inc/bloginfo.asp"-->
<!--#include file="inc/syscode.asp"-->
<!--#include file="inc/function.asp"-->
以上4行是包含文件,他们的功能就相当于C语言中的头文件一样。#include指令将指定文件的内容在ASP服务器执行之前插入到ASP文件中。这些指定的文件一般都是一些在多个页面内都要用到的一些公共函数、变量等网页元素。
<%
dim show
dim 用来定义变量,属于VB语法格式
Application对象用来在应用中共享信息。应用包括在指定的虚拟目录下及其子目录下的所有
asp文件。cachename是一个常量在conn.asp文件中,其值为"oblog"。
if Application(cachename&"index_update")=false and application(cachename&"index")<>"" then
else
end if
if instr(show,"$show_userlogin$")>0 then
end if
response.Write show
最终将页面显示出来
call bottom()
%>
数据库连接
目前大多数网站都用到了数据库,不论是大型的数据库如Oracle、SQL
Server还小型的象Access、MySQL。数据库可以将用户的资料和网站的信息按照一定的形式存储起来,方便信息的检索、组织。
用ASP来进行网站开发的时候一般是用ADO来进行数据库的操作。ADO(ActiveX
Data
Object)是允许用户与数据存储进行交互的组件,它能使客户端应用程序通过OLE
DB提供者访问和操作数据库服务器中的数据。常用的ADO对象主要有下面几个:
Connection
连接数据库
Command
对数据库执行命令
Recordset
执行命令返回所包含的数据集
在oblog中,文件conn.asp包含的就是实现数据库连接的代码。
首先在代码的开始用Option
Explicit语句强制要求显式声明所有变量,即所有的变量都必须用Dim、Public、Private和Redim语句显式声明,如果试图使用未经声明的变量名,则会出现错误,这是一种良好的编程风格。接下来进行数据库连接。
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数据库连接参数:数据库名、用户密码、用户名、连接名(本地用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 "数据库连接出错,请检查连接字串。"
Response.End
End If
if not issqldate then
else
end if
'On Error Resume Next
Set conn = Server.CreateObject("ADODB.Connection")
conn.open ConnStr
If Err Then
End If
在这里作者使用了一个变量issqldate以判断数据库是用Access还是SQL Server,该版本代码中使用的是Access数据库。先定义数据库连接字符串ConnStr,然后创建一个ADODB.Connection对象,最后用Connection的Open方法打开数据库。
判断是否有错误出现,当出现错误时,先清空Connection,然后用Response.End结束页面的处理。
下面代码用来实现系统信息的自动存取。
const cachename="oblog" '缓存对象前缀名
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
if isarray(application(cachename&"info"))=false then
end if
'call ***pplicationValue()
sub closeconn()
end sub
sub ***pplicationValue()
end sub
上面代码中还提供了一个函数closeconn(),用来关闭数据库。我个人感觉conn.asp中所实现的代码有点乱,应该有更加结构化、更清晰的实现方法(比如数据库的打开是否可以用函数来表示),具体怎么实现我也没有想好,在此先记录下来。Written
by ZealotLin
文章引用自:http://www.sczh.com/bbs/oblog/more.asp?name=岂有此李&id=45
前一篇:ASP怎么谈到应用到类的
后一篇:没球打的日子怎么过?

加载中…