always there for you

善于生活,善于学习,这是我和你的目标.

« win2003服务器安全配置整理攻略关于网页下载-C# »

Asp 代码

 

 以下是我常用的Asp代码,现在存起来,以便日后回顾.

1.数据库方面

  Dim cnn  '全局变量

  '--函数名 OpenCnn
  '--作用      用于打开数据库连接 
  Sub OpenCnn()
  Dim cnnstr
   Set cnn = Server.CreateObject("ADODB.Connection")
   cnnstr = "Provider=SQLOLEDB;Data Source=(local);Uid=alwaysthere;Pwd=alwaysthere;Database=alwaysthere_db"
   cnn.Open cnnstr
  End Sub 

'--函数名 ReleaseCnn
'--作用  释放数据库连接

Sub ReleaseCnn()
 If typeName(rs) = "Recordset" Then  '测试rs对象没有释放
  If rs.state=1 Then rs.close  '当rs对象打开时关闭时关闭
  set rs = nothing
 End If
 If typeName(cnn) = "Connection" Then  '测试cnn对象是否释放
  If cnn.state = 1 Then cnn.close  '当cnn对象没有关闭时关闭
  set cnn = nothing
 End If
 End Sub

 ★应注意:
    √.尽量晚打开连接,尽量早关闭连接
        例如: 
               Dim rs,strsql,Results
               strsql="..."
               Set  rs = server.createobject("adodb.recordset")
               Call OpenCnn
               rs.open strsql,cnn,1,1
               Results=rs.getrows()
               Call ReleaseCnn    '--如果取出的数据太多,请不要使用getrows
     √.优化SQL语句或者存储过程,尽量缩短查询运行时间
     √.如果要取出几个表个数据,请一次性取出
     √.查询记录时,如果并不是查询所有字段,请不要使用 Select *.
     √.注意使用rs.open 后的参数,了解第三和第四个参数的详细用法
     √.如果需要在一个代码段中执行多个SQL语句,可以使用存储过程来优化性能.
     √.一个表中的记录不宜太多(比如100万条),把历史记录存入其他的表.
     √.为需要搜索的字段建立索引,必要时可以建立全文索引

2.防SQL注入

    ★发现在请求的Querystring,Form,cookies里有非法字符,就报错
SQL_injdata = "'|;|-"      '--这里放非法字符
SQL_inj = split(SQL_Injdata,"|")

If Request.QueryString<>"" Then
 For Each SQL_Get In Request.QueryString
  For SQL_Data=0 To Ubound(SQL_inj)
   if instr(Lcase(Request.QueryString(SQL_Get)),Sql_Inj(Sql_DATA))>0 Then
    Response.Write "网址中包含非法字符"&Sql_Inj(Sql_DATA)
    Response.End
   End If
  Next
 Next
End If

If Request.Form<>"" Then
 For Each Sql_Post In Request.Form
  For SQL_Data=0 To Ubound(SQL_inj)
   if instr(Lcase(Request.Form(Sql_Post)),Sql_Inj(Sql_DATA))>0 Then
    Response.Write "表单中包含非法字符"&Sql_Inj(Sql_DATA)
    Response.end
   End If
  Next
 Next
End If

If Request.Cookies<>"" Then
 For Each Sql_Cookies In Request.Cookies
  For SQL_Data=0 To Ubound(SQL_inj)
   if instr(Lcase(Request.Cookies(Sql_Cookies)),Sql_Inj(Sql_DATA))>0 Then
    Response.Write "Cookies中包含非法字符"&Sql_Inj(Sql_DATA)&",请在 IE选项-常规 里删除Cookiess再执行操作!"
    Response.end
   End If
  Next
 Next
End If
                     
   ★替换掉非法字符
  Function myreplace(mystring)
   mystring=replace(mystring,"&","&amp;")
   mystring=replace(mystring,"<","&lt;")
   mystring=replace(mystring,">","&gt;")
   mystring=replace(mystring,chr(13),"<br>")
   mystring=replace(mystring,chr(32),"&nbsp;")
   mystring=replace(mystring,chr(9),"&nbsp;&nbsp;&nbsp;&nbsp;")
   mystring=replace(mystring,chr(39),"&acute;")
   mystring=replace(mystring,chr(34),"&quot;")
   myreplace=mystring
 End Function

3.其他
   '--输出客户端对话框
   '--MsgInfo为错误本文,HtD为后续操作,如返回(history.back())
 Function CusRes(MsgInfo,HtD)
  response.Write "<script LANGUAGE='javascript'>"&_
     "alert('"&MsgInfo&"');"&HtD&"</script>"
  response.End()
 End Function

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Copyright www.alwaysthere.com.cn . Some Rights Reserved.

              

Powered By Z-Blog 1.8 Devo Build 80201