#TITLE=ASP常用文法及函數
#INFO
ASP常用的一些文法及自訂函數
#SORT=n
#T= ===ASP常用文法===
#T=============================
#T=資料庫相關
#T= 串連ACCESS資料庫
<%
Dim DBName,Conn
DBName"^!" '定義資料庫路徑及名稱
SET Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(DBName)
%>
#T= 串連MS SQL資料庫
<%
Dim Conn
SET Conn=Server.CreateObject("ADODB.connection")
Conn.Open "PROVIDER=SQLOLEDB;DATA SOURCE=SQL伺服器名稱或IP地址;UID=資料庫登入帳號;PWD=資料庫密碼;DATABASE=資料庫名稱"
%>
#T= 建立記錄集
SET ^!=Server.CreateObject("ADODB.recordset")
#T= 執行SQL命令
RS.Open SQL,conn,1,1
#T= 執行SQL命令
Conn.Execute("^!")
#T= RS直接執行SQL命令
SET RS = Conn.Execute("^!")
#T= 關閉記錄集
RS.CLOSE
SET RS=NOTHING
#T= 關閉資料庫
Conn.Close
SET Conn=Nothing
#T=============================
#T=ServerVariables相關
#T= 取上一頁地址
Request.ServerVariables("HTTP_REFERER")
#T= 取伺服器的名稱1
Request.ServerVariables("SERVER_NAME")
#T= 取伺服器的名稱2
Request.ServerVariables("HTTP_HOST")
#T= 取伺服器IP
Request.ServerVariables("LOCAL_ADDR")
#T= 取使用者IP
Request.ServerVariables("Remote_Host")
#T= 取使用者真實IP1
Request.serverVariables("REMOTE_ADDR")
#T= 取使用者真實IP函數
Function GetRealIP()
GetRealIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
IF(GetRealIP = "")THEN GetRealIP = Request.ServerVariables("REMOTE_ADDR")
End Function
#T= 取伺服器連接埠
Request.ServerVariables("SERVER_PORT")
#T= 取伺服器作業系統
Request.ServerVariables("OS")
#T= 取伺服器的絕對路徑
Request.ServerVariables("APPL_PHYSICAL_PATH")
#T= 取本檔案的絕對路徑1
Requet.ServerVariables("PATH_TRANSLATED")
#T= 取本檔案的絕對路徑2
Server.mappath(Request.ServerVariables("SCRIPT_NAME"))
#T= 取本檔案的相對路徑1
Request.ServerVariables("URL")
#T= 取本檔案的相對路徑2
Request.ServerVariables("SCRIPT_NAME")
#T= 取本檔案的相對路徑3
Request.ServerVariables("PATH_INFO")
#T= 取地址欄後的參數
Request.ServerVariables("QUERY_STRING")
#T= 取伺服器系統資訊
Request.ServerVariables("HTTP_USER_AGENT")
#T= 伺服器組件檢測
<%
Function IsObjInstalled(strClassString)
On Error Resume Next
IsObjInstalled = False
Err = 0
Dim xTestObj
SET xTestObj = Server.CreateObject(strClassString)
IF(0 = Err)THEN IsObjInstalled = True
SET xTestObj = Nothing
Err = 0
End Function
'IF(IsObjInstalled("Persits.Upload")=True)THEN
' Response.Write "支援AspUpload組件"
'ELSE
' Response.Write "不支援AspUpload組件"
'END IF
%>
#T= 取用戶端語言環境
^!Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")
#T= 取用戶端資訊:HTTP_USER_AGENT
^!Request.ServerVariables("HTTP_USER_AGENT")
#T= 取表單(Form)值元素值
Request.Form("^!")
#T= 取URL傳遞的值
Request.QueryString("^!")
#T= 取完整URL地址
Function GetUrl()
GetUrl="http://"&Request.ServerVariables("SERVER_N ... .ServerVariables("URL")
IF(Request.ServerVariables("QUERY_STRING")<>"")THEN GetURL=GetUrl&"?"& Request.ServerVariables("QUERY_STRING")
End Function
#T=============================
#T=自訂函數
#T= 過濾HTML字元
<%
'過濾HTML字元函數
Function HTMLEncode(str)
IF(str <> "")THEN
str = Replace(str, "&", "&")
str = Replace(str, ">", ">")
str = Replace(str, "<", "<")
str = Replace(str, Chr(32), " ")
str = Replace(str, Chr(9), " ")
str = Replace(str, Chr(34), """)
str = Replace(str, Chr(39), "'")
str = Replace(str, Chr(13), "")
str = Replace(str, Chr(10) & Chr(10), "</P><P>")
str = Replace(str, Chr(10), "<BR>")
str = Replace(str, Chr(255), " ")
END IF
HTMLEncode = str
End Function
%>
#T= 檢測上頁是否從本站提交
<%
'檢測上頁是否從本站提交
'返回:True,False
'===============================================================
Function IsSelfRefer()
Dim sHttp_Referer, sServer_Name
sHttp_Referer = CStr(Request.ServerVariables("HTTP_REFERER"))
sServer_Name = CStr(Request.ServerVariables("SERVER_NAME"))
IF(Mid(sHttp_Referer, 8, Len(sServer_Name)) = sServer_Name)THEN
IsSelfRefer = True
ELSE
IsSelfRefer = False
END IF
End Function
%>
#T= 清除所有HTML標記
<%
'清除HTML標記
Function stripHTML(htmlStr)
Dim regEx
SET regEx = New Regexp
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = "<.+?>"
htmlStr = regEx.Replace(htmlStr,"")
htmlStr = Replace(htmlStr, "<","<")
htmlStr = Replace(htmlStr, ">",">")
htmlStr = Replace(htmlStr,chr(10),"")
htmlStr = Replace(htmlStr,chr(13),"")
stripHTML = htmlStr
SET regEx = Nothing
End Function
%>
#T= 取字串長度
<%
'求字串長度函數
Function GetLength(str)
Dim Length
For i=1 to Len(str)
IF(Asc(Mid(str,i,1))<0 or Asc(Mid(str,i,1))>256)THEN
Length=Length+2
ELSE
Length=Length+1
END IF
Next
GetLength=Length
End Function
%>
#T= 截取指定長度字串
<%
'截取指定長度的字串,多餘的用...代替
Function StrLeft(str,strlen)
IF(str = "")THEN
StrLeft = ""
Exit Function
END IF
Dim l,t,c,i
str = Replace(Replace(Replace(Replace(str," "," "),""",chr(34)),">",">"),"<","<")
l=len(str)
t=0
For i=1 to l
c=Abs(Asc(Mid(str,i,1)))
IF(c>255)THEN
t=t+2
ELSE
t=t+1
END IF
IF(t>strlen)THEN
StrLeft = left(str,i) & "..."
Exit For
ELSE
StrLeft = str
END IF
Next
StrLeft = Replace(Replace(Replace(Replace(StrLeft," "," "),chr(34),"""),">",">"),"<","<")
End Function
%>
#T= 擷取安全的提交參數
<%
'===============================================================
'SQL Injection Check
'函數功能:過濾字元參數中的單引號,對於數字參數進行判斷,如果不是數實值型別,則賦值0
'參數意義:str ---- 要過濾的參數
'strType ---- 參數類型,分為字元型和數字型,字元型為"s",數字型為"i"
'===============================================================
Function CheckStr(str,strType)
Dim strTmp
strTmp = ""
IF(strType ="s")THEN
strTmp = Replace(Trim(str),"'","''")
ELSEIF(strType="i")THEN
IF(IsNumeric(str)=False)THEN str=False
strTmp = str
ELSE
strTmp = str
End IF
CheckStr= strTmp
End Function
%>
#T= 過濾不良字元(BadWord)
<%
'過濾不良字元(BadWords)
Function ChkBadWords(fString)
Dim BadWords,bwords,i
BadWords = "我操|操你|操他|你媽的|他媽的|狗|雜種|屄|屌|王八|強姦|做愛|處女|澤民|法輪|法倫|洪志|法輪"
IF(Not(IsNull(BadWords) or IsNull(fString)))THEN
bwords = Split(BadWords, "|")
For i = 0 to UBound(bwords)
fString = Replace(fString, bwords(i), string(Len(bwords(i)),"*"))
Next
ChkBadWords = fString
END IF
End Function
%>
#T= 產生隨機自訂長度密碼
<%
'產生隨機自訂長度密碼
Function makePassword(maxLen)
Dim strNewPass
Dim whatsNext, upper, lower, intCounter
Randomize
For intCounter = 1 To maxLen
whatsNext = Int((1 - 0 + 1) * Rnd + 0)
IF(whatsNext = 0)THEN
'character
upper = 90
lower = 65
ELSE
upper = 57
lower = 48
END IF
strNewPass = strNewPass & Chr(Int((upper - lower + 1) * Rnd + lower))
Next
makePassword = strNewPass
End Function
'Response.Write makepassword(8)
%>
#T= 填入Textarea時保持格式inHTML
<%
'===============================================================
'去除Html格式,用於從資料庫中取出值填入輸入框時
'注意:value="?"這邊一定要用雙引號
'===============================================================
Function inHTML(str)
Dim sTemp
sTemp = str
inHTML = ""
If IsNull(sTemp) = True Then
Exit Function
End If
sTemp = Replace(sTemp, "&", "&")
sTemp = Replace(sTemp, "<br>",chr(13))
sTemp = Replace(sTemp, "<", "<")
sTemp = Replace(sTemp, ">", ">")
sTemp = Replace(sTemp, """, Chr(34))
inHTML = sTemp
End Function
%>
#T= 正則表運算式驗證函式
<%
'正則表運算式驗證函式 patrn-Regex strng-需要驗證的字串
'===============================================================
Function RegExpTest(patrn, strng)
Dim regEx, retVal ' 建立變數。
SET regEx = New RegExp ' 建立Regex。
regEx.Pattern = patrn ' 設定模式。
regEx.IgnoreCase = False ' 設定是否區分大小寫。
retVal = regEx.Test(strng) ' 執行搜尋測試。
RegExpTest = retVal '傳回值,不符合就返回false,符合為true
SET regEx = NOTHING
End Function
%>
#T= 產生隨機字串
<%
'產生隨機字串
Function RndCode()
Dim CodeSet,AmountSet
CodeSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
AmountSet = 62 ' 文字數量
Randomize
Dim vCode(10), vCodes,i
For i = 0 To 9
vCode(i) = Int(Rnd * AmountSet)
vCodes = vCodes & Mid(CodeSet, vCode(i) + 1, 1)
Next
RndCode=vCodes
End Function
%>
#T=============================
#T=FSO相關操作
#T= 判斷目錄是否存在
<%
Function IsFloderExist(strFolderName)
SET FSO=Server.CreateObject("Scripting.FileSystemObject")
IF(FSO.FolderExists(strFolderName))THEN
IsFloderExist = True
ELSE
IsFloderExist = False
END IF
SET FSO=NOTHING
End Function
%>
#T= 建立目錄
<%
Function CreateFolder(strFolderName)
SET FSO=Server.CreateObject("Scripting.FileSystemObject")
IF(FSO.FolderExists(strFolderName) = False)THEN
FSO.CreateFolder(strFolderName)
END IF
SET FSO=NOTHING
END Function
%>
#T= 刪除目錄
<%
Function DeleteFolder(strFolderName)
SET FSO=Server.CreateObject("Scripting.FileSystemObject")
IF(FSO.FolderExists(strFolderName))THEN
FSO.DeleteFolder(strFolderName)
END IF
SET FSO=NOTHING
END Function
%>
#T= 判斷檔案是否存在
<%
Function IsFileExist(strFileName)
SET FSO=Server.CreateObject("Scripting.FileSystemObject")
IF(FSO.FileExists(strFileName))THEN
IsFileExist = True
ELSE
IsFileExist = False
END IF
SET FSO=NOTHING
End Function
%>
#T= 刪除檔案
<%
Function DeleteFile(strFileName)
SET FSO=Server.CreateObject("Scripting.FileSystemObject")
IF(FSO.FileExists(strFileName))THEN
FSO.DeleteFile(strFileName)
END IF
SET FSO=NOTHING
END Function
%>
#T=============================
#T= ASP小偷常用的幾個函數
<%
Function ByteToStr(vIn)
Dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
IF(ThisCharCode < &H80)THEN
strReturn = strReturn & Chr(ThisCharCode)
ELSE
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
END IF
Next
ByteToStr = strReturn
End Function
Function GetHttpPageContent(url,Method,SendStr)
Dim Retrieval
SET Retrieval = Server.CreateObject("Microsoft.XMLHTTP")
With Retrieval
.Open Method, url, False ,"" ,""
.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
.Send(SendStr)
GetHttpPageContent = .ResponseBody
End With
SET Retrieval = Nothing
GetHttpPageContent=ByteToStr(GetHttpPageContent)
End Function
Function RegExpText(strng,regStr)
Dim regEx,Match,Matches,RetStr
SET regEx = New RegExp
regEx.Pattern = regStr
regEx.IgnoreCase = True
regEx.Global = True
SET Matches = regEx.Execute(strng)
For Each Match in Matches
RetStr = RetStr & regEx.Replace(Match.Value,"$1") & ","
Next
RegExpText = RetStr
set regEx=nothing
End Function
Function StreamBytesToBstr(strBody, CodeBase)
Dim objStream
SET objStream = Server.CreateObject("Adodb.Stream")
With objStream
.Type = 1
.Mode = 3
.Open
.Write strBody
.Position = 0
.Type = 2
.Charset = CodeBase
StreamBytesToBstr = .ReadText
.Close
End With
SET objStream = Nothing
End Function
%>