Recently, I have become obsessed with making websites. I used ASP to develop. I found that ASP has many loopholes, and there are many codes that are reused in a website, so I checked some information and found that the idea of classes can be used in ASP, so I I wrote this class. It's not well written, but it's quite practical.
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%
Const Btn_First="<font face='webdings'>9</font>" 'Define the first page button display style
Const Btn_Prev="<font face='webdings'>3</font>" 'Define the previous page button display style
Const Btn_Next="<font face='webdings'>4</font>" 'Define the next page button display style
Const Btn_Last="<font face='webdings'>:</font>" 'Define the last page button display style
Class clsDSJ
Private p_Error,p_IpAdd,p_Debug,Errs
Private p_InvaildWords
Private tmpStr
Private objConnDivPage,rsDivPage,p_GetSQL,p_nPage
Private p_rsPageSize,p_intCurPage,p_strURL,p_intTotalPage,p_intTotalRecords
Private p_dbType,p_dbFolder,p_DataBase,dbPath,p_dbUserID,p_dbPassword,p_sqlDataSource
Private param_DataBase,param_dbUserID,param_dbPassword,param_dbType
'Property
Public objConn,ConnStr,SQLQueryNum
Public logDB,WebURL
Public SaveLog
Public ErrCodes,ErrCode,ErrMsg
Public ScriptFolder,ScriptName
Private Sub Class_Initialize
p_Debug=True
ClientConnected
SaveLog=True
ErrMsg=""
p_Error=0
SQLQueryNum=0
Rem database connection initialization
dbType=12 'Take 1x——Access, 2x——SQL; x1——OLEDB, x2——ODBC
dbFolder="Data"
DataBase="zhubizidatabase.MDB"
dbUserID=""
dbPassword=""
sqlDataSource=""
p_rsPageSize=10
'p_nPage=2
If request("page")="" Then
p_intCurPage=1
ElseIf NOT IsNumeric(request("page")) Then
p_intCurPage=1
ElseIf CInt(Trim(request("page")))<1 Then
p_intCurPage=1
Else
p_intCurPage=CInt(Trim(request("page")))
End If
WebURL=" http://blog.csdn.net/alonesword/ "
tmpStr=Request.ServerVariables("PATH_INFO")
tmpStr=Split(tmpStr,"/",-1)
ScriptName=Lcase(tmpStr(UBound(tmpStr)))
ScriptFolder=LCase(tmpStr(UBound(tmpStr)-1)) & "/"
p_InvaildWords=" select|update|delete|insert|@|--|;|'|#|%|xp|cmd|shell "
End Sub
Private Sub Class_terminate()
If IsObject(rsDivPage) Then rsDivPage.Close:Set rsDivPage=Nothing
If IsObject(objConn) Then objConn.Close:Set objConn=Nothing
End Sub
Public Property Get Version
Version="DSJ 1.1.0 Beta"
End Property
Public Property Get Error
Error=p_Error
End Property
Public Property Get Debug
Debug=p_Debug
End Property
Public Property Let Debug(BoolDebug)
If BoolDebug Then
p_Debug=True
Else
p_Debug=False
End If
End Property
Public Property Get dbType
dbType=p_dbType
End Property
Public Property Let dbType(lngdbType)
If IsNumeric(lngdbType) Then
p_dbType=lngdbType
Else
p_dbType=12
End If
End Property
Public Property Get dbFolder
dbFolder=p_dbFolder
End Property
Public Property Let dbFolder(strFolder)
If Right(strFolder,1)="" OR Right(strFolder,1)="/" Then
p_dbFolder=strFolder
Else
If (NOT IsNull(strFolder)) OR (strFolder<>"") Then
p_dbFolder=strFolder & ""
Else
p_dbFoler=""
End If
End If
End Property
Public Property Get Database
Database=p_dataBase
End Property
Public Property Let Database(strDataBase)
p_dataBase=strDatabase
End Property
Public Property Get dbUserID
dbUserID=p_dbUserID
End Property
Public Property Let dbUserID(strDataBaseUserID)
p_dbUserID=strDataBaseUserID
End Property
Public Property Get dbPassword
dbPassword=p_dbPassword
End Property
Public Property Let dbPassword(strDataBasePassword)
p_dbPassword=strDataBasePassword
End Property
Public Property Get SQLDataSource
SQLDataSource=p_sqlDataSource
End Property
Public Property Let SQLDataSource(strSQLDataSource)
If strsqlDataSource<>"" Then
If Left(p_dbType,1)=2 Then
p_sqlDataSource=strSQLDataSource
Else
AddErrorCode(110)
p_sqlDataSource=""
End If
End If
End Property
Public Property Let PageSize(int_PageSize)
If IsNumeric(Int_Pagesize) Then
p_rsPageSize=CLng(int_PageSize)
End If
End Property
Public Property Get PageSize
If p_rsPageSize="" or NOT IsNumeric(p_rsPageSize) Then
PageSize=8
Else
PageSize=p_rsPageSize
End If
End Property
Public Property Get GetRs()
If NOT Debug Then On Error Resume Next
If NOT IsObject(objConn) Then ConnectionDataBase dbDataBase,dbUserID,dbPassword,dbType
If NOT IsObject(rsDivPage) Then
Set rsDivPage=Server.createobject("adodb.recordset")
rsDivPage.Open GetSQL,objConn,1,1
rsDivPage.PageSize=PageSize
If not(rsDivPage.eof and rsDivPage.BOF) Then
If p_intCurPage>rsDivPage.PageCount Then
p_intCurPage=rsDivPage.PageCount
End If
rsDivPage.AbsolutePage=p_intCurPage
End If
If Err Then
Err.Clear
rsDivPage.Close
Set rsDivPage=Nothing
If SaveLog Then
tmpMsg="Error connecting to the database, please check whether your query code is correct.<br>For security reasons, only this message is displayed. To view detailed error messages, please set dsj.Debug=True"
Response.Write SaveSQLLog(cmd,tmpMsg)
Else
Response.Write "Error connecting to database, please check whether your query code is correct."
End If
Response.End()
End If
SQLQueryNum=SQLQueryNum+1
Set GetRs=rsDivPage
End If
End Property
Public Property Let GetSQL(str_sql)
If str_sql<>"" Then
p_GetSQL=str_sql
Else
AddErrorCode(111)
If Debug Then ShowErrMsg()
p_GetSQL="The Property of GetSQL is NULL."
End If
End Property
Public Property Get GetSQl()
GetSQL=p_GetSQL
End Property
'******************************
'Name: gotTopic
'Parameters: str, strlen
'Return value: restricted string
'Creation time: March 3, 2005
'Function: limit the restricted string
'******************************
Public Function GotToPic(str,strlen)
Rem limits the display length of the string to a certain range
Dim i,StringLen,CharSize,EchoCharType
StringLen=len(str)
CharSize=0
For i=1 to StringLen
EchoCharType=Abs(Asc(Mid(str,i,1)))
If EchoCharType>255 Then
CharSize=CharSize+2
Else
CharSize=CharSize+1
End If
If CharSize>strlen Then
gotTopic=Left(str,i) & "..."
Exit For
Else
gotTopic=str & ""
End If
Next
End Function
'******************************
'Name: ChkInvStr
'Parameter: Str
'Return value: True/False
'Creation time: May 2, 2005
'Function: Check whether the parameters have illegal characters
'******************************
Public Function ChkInvStr(Str)
Rem defines illegal characters that need to be filtered
Dim InvaildWord,inWords,i
Str=CStr(Str)
ChkInvStr=False
If Len(Replace(p_InvaildWords,Chr(0),""))<1 Then
AddErrorCode(103) 'invaildwords is null
Exit Function
Else
If Instr(1,p_Invaildwords,"|")>0 Then
InvaildWord=Split(p_InvaildWords,"|")
inWords=LCase(Trim(Str))
For i=LBound(InvaildWord) To UBound(InvaildWord)
If Instr(inWords,InvaildWord(i))>0 Then
p_Error=1982
AddErrorCode(105)
ChkInvStr=True
Exit Function
End If
Next
Else
AddErrorCode(104) '"|" is requried.
Exit Function
End If
End If
End Function
'******************************
'Name: GetIP
'Parameter: NULL
'Return value: NULL
'Creation time: May 3, 2005
'Function: Get the user's IP address
'******************************
Public Function GetIP()
p_IpAdd=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If IsNull(p_IpAdd) OR p_IpAdd="" Then
p_IpAdd=Request.ServerVariables("REMOTE_ADDR")
End If
GetIp=p_IpAdd
End Function
'******************************
'Name: IPAddress
'Parameter: sip
'Return value: County, City
'Function: typesetting and conversion of data content
'Creation time: May 6, 2005
'Source: www.downcodes.com
'******************************
Public Function IPAddress(sip)
Dim IPConnStr,IPConn,IPAddressDB
Dim str1,str2,str3,str4
Dim num
Dim country,city,address
Dim IPRs,SQL
address="unknown"
If IsNumeric(Left(sip,2)) Then
If sip="127.0.0.1" Then sip="192.168.0.1"
str1=Left(sip,InStr(sip,".")-1)
sip=mid(sip,instr(sip,".")+1)
str2=Left(sip,instr(sip,".")-1)
sip=Mid(sip,InStr(sip,".")+1)
str3=Left(sip,instr(sip,".")-1)
str4=Mid(sip,instr(sip,".")+1)
If isNumeric(str1)=0 or isNumeric(str2)=0 or isNumeric(str3)=0 or isNumeric(str4)=0 Then
Else
num=CLng(str1)*16777216+CLng(str2)*65536+CLng(str3)*256+CLng(str4)-1
IPAddressDB = "DSJ_Ipaddress.mdb"
IPConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(p_dbFolder & IPAddressDB)
Set IPConn = Server.CreateObject("ADODB.Connection")
IPConn.Open IpConnStr
country="Asia"
city=""
sql="select top 1 country,city from DSJ_IPAddress where ip1 <="&num&" and ip2 >="&num&""
Set IPRs=IPConn.execute(sql)
If Not(IPRs.EOF And IPRs.bof) Then
country=IPRs(0)
city=IPRs(1)
End If
IpRs.Close:Set IPRs=Nothing
IPConn.CLose:Set IPConn = Nothing
SqlQueryNum = SqlQueryNum+1
End If
IPAddress=country&city
End If
End Function
'******************************
'Name: ClientConnected
'Parameter: NULL
'Return value: NULL
'Creation time: May 5, 2005
'Function: Get whether the user is connected to the server
'******************************
Public Sub ClientConnected()
If Response.IsClientConnected then
Response.Flush()
Else
Response.End()
End If
End Sub
'******************************
'Name: ChkPost
'Parameter: NULL
'Return value: True/False
'Creation time: May 5, 2005
'Function: Check the source of submitted data
'******************************
Public Function ChkPost()
Dim Server_v1,Server_v2
ChkPost=False
Server_v1=CStr(Request.ServerVariables("HTTP_REFERER"))
Server_v2=CStr(Request.ServerVariables("SERVER_NAME"))
If Mid(Server_v1,8,Len(Server_v2))=Server_v2 Then ChkPost=True
End Function
'******************************
'Name: AddErrorCode
'Parameter:ErrCode
'Return value: ErrCodes
'Creation time: May 3, 2005
'Function: Add error codes to ErrCodes
'******************************
Public Sub AddErrorCode(ErrCode)
If ErrCodes="" Then
ErrCodes=ErrCode
Else
ErrCodes=ErrCodes & "," & ErrCode
End If
End Sub
'******************************
'Name: ShowErrMsg
'Parameter: NULL
'Return value: NULL
'Creation time: May 3, 2005
'Function: Display detailed error information
'******************************
Public Function ShowErrMsg()
If NOT p_Debug Then On Error Resume Next
If ErrCodes<>"" Then
iErrCodes=Split(ErrCodes,",",-1,Binary)
For i=LBound(iErrCodes) To UBound(iErrCodes)
tmpErrCode=CLng(iErrCodes(i))
If IsNumeric(tmpErrCode) Then
ErrMsg=ErrMsg & ErrDetails(tmpErrCode)
End If
Next
End If
ShowErrMsg=ErrMsg
End Function
'******************************
'Name: ConnectionDataBase
'Parameters:DataBase,p_dbUserID,p_dbPassword,p_dbType
'Return value: NULL
'Function: connect to the database
'Creation time: May 6, 2005
'******************************
Public Function ConnectionDataBase(param_DataBase,param_dbUserID,param_dbPassword,param_dbType)
If Not p_Debug Then On Error Resume Next
ClientConnected
If IsNumeric(param_dbType) Then
Set objConn=Server.CreateObject("ADODB.Connection")
objConn.ConnectionTimeOut=10 'Set the connection timeout to 10 seconds
dbPath=Server.MapPath(p_dbFolder¶m_DataBase)
'Response.Write dbPath
Select Case param_dbType
Case 11: 'Access Data Base(OLEDB)
ConnStr="Provider=Microsoft.Jet.Oledb.4.0;Data Source="&dbPath&";User ID="¶m_dbUserID&";Password="¶m_dbPassword&""
Case 21: 'SQL Server (OLEDB)
ConnStr="Provider=SQLOLEDB;Initial Catalog="¶m_DataBase&";Data Source="&p_sqlDataSource&";User ID="¶m_dbUserID&";Password="¶m_dbPassword&""
Case 12: 'Access Data Base(ODBC)
ConnStr="Driver={Microsoft Access Driver (*.mdb)};DBQ="&dbPath&";User ID="¶m_dbUserID&";Password="¶m_dbPassword
Case 22: 'SQL Server (ODBC)
ConnStr="Driver={SQL Server};DataBase="¶m_DataBase&";Server="&p_sqlDataSource&";User ID="¶m_dbUserID&";Password="¶m_dbPassword&""
Case Else:
AddErrorCode(100)
If p_Debug Then Response.Write ShowErrMsg()
'Exit Function
End Select
objConn.ConnectionString=ConnStr
objConn.Open
Set ConnectionDataBase=objConn
If Err Then
Err.Clear
If SaveLog Then
tmpMsg="An error occurred during database connection, please check whether your query code is correct.<br>For security reasons, only this information is displayed. To view detailed error information, please set dsj.Debug=True"
cmd="Execute the command of :"¶m_dbtype
Response.Write SaveSQLLog(cmd,tmpMsg)
Else
Response.Write "An error occurred while connecting to the database. Please check whether your query code is correct."
End If
AddErrorCode(101)
Set objConn=Nothing
Response.End()
End If
End If
End Function
'******************************
'Name: SaveSQLLog
'Parameters:sCommand,Msg
'Return value: msg
'Function: store logs
'Creation time: May 6, 2005
'******************************
Public Function SaveSQLLog(sCommand,Msg)
If Not p_Debug Then On Error Resume Next
logDB="DSJ_SQLLog.mdb"
'ConnectionDataBase logDB,p_dbUserID,p_dbPassword,12
logConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(p_dbFolder & logDB)
Set logConn = Server.CreateObject("ADODB.Connection")
logConn.Open logConnStr
If SaveLog Then
Dim logCmd
logCmd="INSERT INTO DSJ_SQL_Log (Command,ScriptName,logUser,IP) VALUES "
logCmd=logCmd & "('" & Replace(Left(sCommand,255),"'","''") &"','" & (ScriptFolder & ScriptName) &"','" & Session("User ") & "','"& GetIP & "')"
'Response.Write "Need to execute command is:<br><font color=red>" & logCmd & "</font><BR>"
logConn.Execute(logCmd)
End If
logConn.Close
Set logConn=Nothing
SaveSQLLog=msg
SQLQueryNum=SQLQueryNum+1
End Function
'******************************
'Name: ExecuteCmd
'Parameter: cmd
'Return value: RecordSet
'Function: Return the recordset created by cmd
'Creation time: May 6, 2005
'******************************
Public Function ExecuteCmd(cmd)
If NOT IsObject(objConn) Then ConnectionDataBase p_DataBase,p_dbUserID,p_dbPassword,p_dbType
If NOT p_Debug Then On Error Resume Next
ClientConnected
Set ExecuteCmd=objConn.Execute(cmd)
If Err Then
Err.Clear
objConn.Close
Set objConn=Nothing
If SaveLog Then
tmpMsg="An error was found when querying data, please check whether your query code is correct.<br>For security reasons, only this information is displayed. To view detailed error information, please set dsj.Debug=True"
Response.Write SaveSQLLog(cmd,tmpMsg)
Else
Response.Write "An error was found when querying data. Please check whether your query code is correct."
End If
Response.End()
End If
SQLQueryNum=SQLQueryNum+1
End Function
'******************************
'Name: HTMLEncode
'Parameter:fString
'Return value: HTMLEncode
'Function: typesetting and conversion of data content
'Creation time: May 6, 2005
'******************************
Public Function HTMLEncode(fString)
If Not IsNull(fString) Or fString<>"" Then
fString=Replace(fString,"<","<")
fString=Replace(fString,">",">")
fString=Replace(fString,Chr(9)," ") 'Italic font
fString=Replace(fString,Chr(13),"")
fString=Replace(fString,Chr(32)," ") 'Convert spaces
fString=Replace(fString,Chr(34),""") 'Convert double quotes
fString=Replace(fString,Chr(39),"'") 'Convert single quotes
fString=Replace(fString,Chr(10),"</p><p>") 'Convert to paragraph format
fString=Replace(fString,Chr(10),"<BR> ") 'Convert to the next line
fString=Replace(fString,Chr(13),"<BR>")
fString=Replace(fSting,vbCrlf,"<BR>")
'fString=LinkFriend(fString)
'fString=ChkBadWords(fString)
HTMLEncode=fString
End If
End Function
'******************************
'Name: LinkFriend
'Parameter:fContent
'Return value: LinkFriend
'Function: Replace friendly connections
'Creation time: May 6, 2005
'******************************
Public Function LinkFriend(fContent)
If NOT p_Debug Then On Error Resume Next
If NOT IsObject(objConn) Then ConnectionDataBase DataBase,dbUserID,dbPassword,dbType
Set rsFriend=ExecuteCmd("Select Title,URL From dsj_FriendLink")
If rsFriend.Eof And rsFriend.Bof Then
Else
Do While NOT rsFriend.Eof
strTitle=rsFriend.Fields.Item("Title")
URl=rsFriend.Fields.Item("URL")
If LCase(Left(URL,1))<>"h" Then URL=Replace(URl,Left(URL,1),"")
strLink="<a target='_blank' title='" & strTitle &"' href='" & URL & "'>"
strLink=strLink&strTitle&"</a>"
If Instr(fContent,strTitle)>0 Then fContent=Replace(fContent,strTitle,strLink)
rsFriend.MoveNext
Loop
LinkFriend=fContent
End If
rsFriend.Close
Set rsFriend=Nothing
SQLQueryNum=SQLQueryNum+1
End Function
'************************************************ ******************
'Name: ShowPage
'Parameter: NULL
'Return value: NULL
'Function: Display the paging record navigation
'illustrate:
'Original work: zykj2000 Website: http://bbs.513soft.net
' Modification: Alonesword Website: http://blog.csdn.net/alonesword/
'Time: May 7, 2005
'************************************************ *****************
Public Sub ShowPage()
Dim str_tmp
p_intTotalRecords=rsDivPage.RecordCount
If p_intTotalRecords<=0 Then
p_Error=p_Error & "The total number of records is zero, please enter data"
Call ShowError()
End If
If p_intTotalRecords <=PageSize THen
p_intTotalPage=1
Else
If p_intTotalRecords mod PageSize =0 Then
p_intTotalPage = CLng(p_intTotalRecords / PageSize * -1)*-1
Else
p_intTotalPage = CLng(p_intTotalRecords / PageSize * -1)*-1+1
End If
End If
If p_intCurPage>p_intTotalPage Then
p_intCurPage=p_intTotalPage
End If
Response.Write ShowFirstPrv
showNumBtn
Response.Write ShowNextLast&" "
Response.Write ShowPageInfo
response.write str_tmp
End Sub
Private Function ShowFirstPrv()
Dim Str_tmp,int_prvpage
int_prvpage=p_intCurPage-1
If int_prvpage<1 Then int_prvpage=1
Btn_FirstLink="<a title='Page 1' href='"&AddnPageURL&"1'>"&Btn_First
Btn_PrevLink="<a title='Page"&int_prvpage&" href='"&AddnPageURL&p_intCurPage-1&"'>"&Btn_Prev
If p_intCurPage=1 Then
str_tmp=Btn_FirstLink&"</a> "&Btn_PrevLink&"</a> "
Else
int_prvpage=p_intCurPage-1
str_tmp=""&Btn_FirstLink&"</a> "& Btn_PrevLink&"</a> "
End If
ShowFirstPrv=str_tmp
End Function
Private Function ShowNextLast()
Dim str_tmp,int_Nextpage
int_NextPage=p_intCurPage+1
If p_intCurPage+1>p_intTotalPage Then int_NextPage=p_intTotalPage
Btn_NextLink="<a title='Page"&int_NextPage&"' href='"&AddnPageURL&p_intCurPage+1&"'>"&Btn_Next
Btn_LastLink="<a title='Page"&p_intTotalPage&" href='"&AddnPageURL&p_intTotalPage&"'>"&Btn_Last
If p_intCurPage>=p_intTotalPage Then
str_tmp=Btn_NextLink & "</a> " & Btn_LastLink&"</a>"
Else
Int_NextPage=p_intCurPage+1
str_tmp=""&Btn_NextLink&"</a> "& Btn_LastLink&"</a>"
End If
ShowNextLast=str_tmp
End Function
Private Function showNumBtn()
Dim i,str_tmp
Dim PageLink
str_tmp=""
For i=1 to p_intTotalPage Step 1
PageLink=" <a title='Page"&i&" href='"&AddnPageURL&i&"'>"&i&"</a> "
If i=p_intCurPage Then PageLink=" <a title='Page"&i&" href='"&AddnPageURL&i&"'><font color=red>"&i&"</font></a> "
Response.Write PageLink
Next
showNumBtn=str_tmp
End Function
Private Function ShowPageInfo()
Dim str_tmp
str_tmp="Page:"&p_intCurPage&"/"&p_intTotalPage&"Page total"&p_intTotalRecords&"records"&p_rsPageSize&"records/per page"
ShowPageInfo=str_tmp
End Function
Private Function AddnPageURL()
Dim i,j,search_str,result_url
search_str="page="
str_params=Request.ServerVariables("QUERY_STRING")
If str_params="" Then
result_url=ScriptName & "?page="
Else
If InstrRev(str_params,search_str)=0 Then
result_url=ScriptName & "?" & str_params &"&page="
Else
j=InstrRev(str_params,search_str)-2
If j=-1 Then
result_url=ScriptName & "?page="
Else
str_params=Left(str_params,j)
result_url=ScriptName & "?" & str_params &"&page="
End If
End If
End If
AddnPageURL=result_url
End Function
'************************************************ *****************
Public Function GetName(Options)
Dim tmpName
tmpName=Request.ServerVariables("PATH_INFO")
arrTmpName=Split(tmpName,"/")
If Options=0 Then GetName=Server.Mappath(".")&GetName(1)
If Options=1 Then GetName=LCase(arrTmpName(UBound(arrTmpName)))
If Options=2 Then GetName=LCase(arrTmpName(UBound(arrTmpName)-1))&"/"
End Function
End Class
%>