第四节:君临沙场、锦囊相助
通过以上三节修炼,君若仔细攻读,定已胸有成竹,跃跃欲试,此时想必已经动起手来,或者是思路很清晰却不知程序该如何写,若真是如此,不必急噪,笔者先将平生所学一些常用“兵法”一一列出,定可使汝茅塞顿开
1:错误提示函数(需要时使用,可弹出对话框给与用户错误提醒,而后自动退回上一页,此函数建议保存为一通用文件,需要时<!--#include file="publicfun.aspx"-->一下即可使用)
<script language=vb runat='server'>
sub t2(tstr2 as string)
response.write (replace("<script language=vbs> msgbox " & chr(34) & tstr2 & chr(34) & ",16," & chr(34) & "错误提示" & chr(34) & ":history.back()</script>","<","<")):response.end
end sub
</script>
2:一般数据检测函数(修改XML标签属性时建议使用,用于一般检测,至于用户帐号等还需要更严密的数据过滤)
<script language=vb runat='server'>
sub jiancha(requeststr as string) '本例过滤'"<>&*,如有疏漏请补之
dim array1(5) as string
array1(0)="'":array1(1)=chr(34):array1(2)="<":array1(3)=">":array1(4)="&":array1(5)="*"
dim tempi as integer
for tempi=0 to ubound(array1)
if instr(requeststr,array1(tempi))<>0 then response.write (replace("<script language=vbs> msgbox " & chr(34) & "参考数据 " & requeststr & " 不可以含有禁止符号 [" & array1(tempi) & "] ,自动返回请更正" & chr(34) & ",16," & chr(34) & "错误提示" & chr(34) & vbcrlf & "history.back()</script>","<","<")):response.end
next
end sub
</script>
3:asp.net拒绝跨站提交注入(可在会员注册、会员修改页等表单较多的页使用)
<%
if lcase(mid(request.ServerVariables("HTTP_REFERER"),8,len(request.ServerVariables("SERVER_NAME"))))<>lcase(request.ServerVariables("SERVER_NAME")) then t2("拒绝跨站提交!")
%>
4:向目标XML文件添加新节点
<%@import namespace='system.xml'%>
<%
dim userdom1=new xmldocument,userdom2,userdom3
userdom1.load (server.mappath("user.xml")) '装载需要操作的XML文件
userdom2=userdom1.selectsinglenode("alluser") '操作游标指向alluser节点,句柄给userdom2变量
userdom3=userdom1.createElement("fyw") '新创建一名为fyw的节点,并将句柄给userdom3变量
userdom3.setAttribute("name","风云舞") '为userdom3添加节点属性,新属性名为name,值为风云舞
userdom3.setAttribute("pass","123")
userdom3.innerText="测试" '设置userdom3节点所包含的数据
userdom2.AppendChild(userdom3) 'userdom2即alluser节点下添加userdom3所描述的fyw节点
userdom1.save (server.mappath("user.xml")) '将变动后的新XML数据保存到user.xml
%>
5:修改XML节点
<%@import namespace='system.xml'%>
<%
dim userdom1=new xmldocument,userdom2
userdom1.load (server.mappath("user.xml"))
userdom2=userdom1.selectsinglenode("alluser").getElementsBytagname("fyw") '操作游标指向alluser节点下的fyw节点
userdom2(0).setAttribute("name","风云舞") '设定fyw节点数组的第一个,将其name属性值修改为风云舞
userdom2(0).innerText="测试" '设定fyw节点数组的第一个,将其所包含的数据改为测试
userdom1.save (server.mappath("user.xml")) '将变动后的新XML数据保存到user.xml
%>
6:查找判断XML节点是否存在
<%@import namespace='system.xml'%>
<%
dim userdom1=new xmldocument
userdom1.load (server.mappath("user.xml")) '装载需要操作的XML文件
'以下正是本人为什么要用“用户帐号”标识用户唯一身份的目的,查找方便呀:)
if userdom1.selectsinglenode("alluser").getElementsBytagname("fyw").count<>0 then t2("fyw节点以存在")
%>
7:批量添加XML节点(在编写回复帖子时,用第4例提到的方法一个一个填加节点很麻烦,这时可以考虑用此方法批量填加)
<%@import namespace='system.xml'%>
<%
dim filedom=new xmldocument,filedom2
filedom.load(server.mappath("data\" & request.querystring("dex"))) 'request.querystring("dex")=6.xml
filedom2=filedom.createElement("reply")
'以下这种方法很方便,而且对XML文件的结构排版也很好,合理的空格和回车可以表现良好的XML文档结构
filedom2.innerXml=vbcrlf & " <anthor>" & session("who") & "</anthor>" & vbcrlf & " <date>" & now & "</date>" & vbcrlf & " <gengxindate>" & now & "</gengxindate>" & vbcrlf & " <body>" & neirong.value & "</body>" & vbcrlf & " "
filedom.selectsinglenode("document/record").appendChild(filedom2)
filedom.save(server.mappath("data\" & request.querystring("dex"))) 'request.querystring("dex")=6.xml
%>
8:删除XML节点
<%@import namespace='system.xml'%>
<%
dim userdom1=new xmldocument,userdom2
userdom1.load (server.mappath("user.xml"))
userdom1.DocumentElement.RemoveChild (userdom1.selectsinglenode("alluser/fyw")) '删除alluser节点下的所有fyw节点
userdom1.save (server.mappath("user.xml"))
%>
9:创建新的XML文件(可以创建任何类型后缀的文件,不局于XML文件)
<%@import namespace='system.io'%>
<%
dim newfile=new StreamWriter(server.mappath("data\" & new DirectoryInfo(server.mappath("data")).getfiles().length+1 & ".xml"),false,System.Text.Encoding.Default)
'以上我们指定默认编码方式为System.Text.encoding.default,或者可改为System.Text.encoding.GetEncoding("gb2312"),不然将以UTF8编码那样XML含有中文就无法正常工作了
newfile.write ("<?xml version=" & chr(34) & "1.0" & chr(34) & " encoding=" & chr(34) & "gb2312" & chr(34) & "?>" & _
vbcrlf & "<?xml-stylesheet type='text/xsl' href='../file.aspx?dex=" & filelength+1 & ".xml'?>" & vbcrlf & _
"<document>" & vbcrlf & " <record>" & vbcrlf & " <anthor>" & session("who") & "</anthor>" & vbcrlf & _
" <title>" & server.HTMLEncode(biaoti.value) & "</title>" & vbcrlf & " <date>" & now & "</date>" & vbcrlf & _
" <gengxindate>" & now & "</gengxindate>" & vbcrlf & " <body>" & server.HTMLEncode(neirong.text) & "</body>" & vbcrlf & _
" </record>" & vbcrlf & "</document>")
newfile.close:newfile=nothing
%>
10:删除指定文件
<%@import namespace='system.io'%>
<%file.delete(server.mappath("data\2.xml"))%>
11:用cookies实现7秒内不许重复灌水
<%
if not request.cookies("lshdicbbs") is nothing then
if isdate(request.cookies("lshdicbbs")("guanshui"))=false then response.cookies("lshdicbbs")("guanshui")=now
if datediff("s",request.cookies("lshdicbbs")("guanshui"),now)<7 then response.write ("7秒内禁止重复发贴灌水"):response.cookies("lshdicbbs")("guanshui")=now:response.end
end if
response.cookies("lshdicbbs")("guanshui")=now
%>
12:用正则表达式限制用户提交的数据必须为英文字母(有兴趣可延伸强化为支持英文字母+数字的形式)
<%
dim name1=request.form("username")
if regex.replace(name1,"[a-z]+","",RegexOptions.IgnoreCase)<>"" then t2("帐号名必须使用A-Za-z范围的英文字母")
%>
13:扩展提示工具(早先原创脚本,可根据需要修改)
<div style='position:absolute;left:0;top:0;border-bottom:1 solid green;border-right:1 solid green;border-left:1 solid cccccc;border-top:1 solid cccccc;display:none;z-index:500;background-color:#FFF7FF;padding:2;white-Space:nowrap;table-Layout:fixed;' id=showdiv></div>
<script>
var oldtext="加速变量",colors1=new Array("#FFECD5","#FFF7FF","#FFFFEB","white","#F5FFEB","#EEFAFF","#FFFFEE","#EDFFFC")
function document.onmousemove(){
try{
if(event.srcElement.getAttribute('lshdic'))
{
showdiv.style.left=event.x-3;showdiv.style.top=event.y+document.body.scrollTop+18;if(event.srcElement.lshdic!=oldtext){oldtext=event.srcElement.lshdic;showdiv.innerText=oldtext;showdiv.style.backgroundColor=colors1[Math.round(Math.random()*colors1.length)]};if(showdiv.style.display=='none')showdiv.style.display=''
}else{if(showdiv.style.display=='')showdiv.style.display='none';}}catch(e){}
}
</script>
<a href="http://www.lshdic.com" lshdic='欢迎访问作者网站'>蓝丽网</a>
<a href="http://www.lshdic.com/bbs" lshdic='欢迎访问蓝丽技术论坛 2003-12-6 10:31:32'>蓝丽技术论坛</a>