ASP常用代码段之十
1:长文章自动分页类
001to1to0
2:asp登陆窗口代码,含数据库的连接
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<%
dim conn
set conn=Server.createobject("adodb.connection")
conn.open "provider=microsoft.jet.oledb.4.0;data source="&server.mappath("jixie.mdb")
If err.number<>0 then
Response.write("连接数据库时产生错误!")
Response.End
End if
%>
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>数控机床结构设计系统</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 36px}
.STYLE2 {font-size: 36px; color: #FF0000; }
.STYLE6 {
font-size: 24px;
color: #000000;
}
.STYLE8 {font-size: 14px}
.STYLE9 {font-size: 16px; }
body {
background-image: url(image/001.jpg);
}
-->
</style>
</head>
<body >
<label>
<form id="form1" name="form1" method="post" action="">
<div align="center">
<h1 class="STYLE1"><br />
<br />
</h1>
<h1 class="STYLE2"><span class="STYLE6">数控机床结构设计系统</span><br />
<br />
<span class="STYLE8">*您没登陆,不具有任何操作权限,请先登陆!</span></h1>
</div>
</form>
<div align="center"><br />
<form id="form3" name="form3" method="post" action="">
<p class="STYLE9">用户名
<input name="user" type="text" id="user" />
</p>
<p class="STYLE9">密 码
<input name="pass" type="password" id="pass" />
</p>
<p class="STYLE9">
<input type="submit" name="Submit" value="提交" />
<input type="reset" name="reset" value="重置" />
</p>
</form>
</div>
</label>
<div align="center">
<%
dim user,pass,power
user=request("user")
pass=request("pass")
power=request("power")
if user="" or pass="" then
response.write "用户名或密码不能为空!"
response.end
end if
if power=0 then
set rs=conn.execute("select * from dljm where and password='"&pass&"'")
else
response.write "你的密码或用户名有误,请重新登陆"
end if
%>
<%
if not rs.eof then
if power=0 then
session("name")=user
response.redirect"1.asp"
end if
else
response.write "你的用户名或密码有误,请重新登陆"
response.end
end if
%>
</div>
</body>
</html>
3:对非法图片的删除
lastpath="已经上传的图片路径"
sFile=server.mappath(lastpath)
set MyFile=server.CreateObject("Scripting.FileSystemObject")
set MyText=MyFile.OpenTextFile(sFile, 1) '读取文本文件
sTextAll=lcase(MyText.ReadAll)
MyText.close
sStr=".getfolder,.createfolder,.deletefolder,.createdirectory,.deletedirectory,.saveas,wscript.shell,script,.encode.,重命名,修改,属性,文件,浏览器,新建,复制,成功,参数错误,服务器,空间,下载,http,create,delete,fso,set,select,execute,response"
sNoString=split(sStr,",")
for i=0 to ubound(sNoString)
if instr(sTextAll,sNoString(i)) then
SET fs=server.CreateObject("Scripting.FileSystemObject")
if FS.FileExists(sFile) then
FS.DeleteFile(sFile)
end if
set fs=nothing
response.Write "<script>alert('上传操作失败,非法图片'),window.history.go(-1);</script>"
4:使用ASP重启服务器
大家知道直接使用ASP是不能够重启服务器的,这时我们需要制作一个组件来实现功能,ASP通过这个组件调用系统API,然后按照不同的重启和关机方式进行操作!
下面先说COM的制作,在VB中新建一工程,当然是AceiveX dll的!
1)先修改工程属性,在工程属性窗口将工程名称改为system,在类模块窗口将模块名称改为contral,保存工程;
2)然后添加一个模块,用来声明需要使用的API和常数!下面是模块中的内容!
Declare Function ExitWindowsEx Lib "user32 " (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
3)在类模块中输入下列代码:
Public Function reboot(atype As Variant)
t& = ExitWindowsEx(1, atype)
End Function
该函数需要传入一个参数,用来控制启动方式,这个参数是从ASP程序中传入的,等一下就可以看到具体的使用方法!
编写完上面的代码,保存工程,将工程编译为system.dll。下面就写ASP程序!
ASP中的代码非常简单:
<%
atype=request( "atype ")
set sys=server.CreateObject ( "system.contral ")
sys.reboot(atype)
%>
其中atype可以使用0、1、2、4,0、1和4均为关机,2用来重启。写完后执行ASP就可以了
5:asp自动生成html并覆盖原html
<%
dim read,Curl,content
Function getHTTPPage(url)
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
End function
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
Curl=""
read=getHTTPPage(Curl)
if read<>"" then
content=read
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("index.html")
Set Site_Config=FSO.CreateTextFile(Filen,true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
Response.Write("<script>alert('已经成功生成首页!')</script>")
end if
%>
6:asp生成html代码
<%
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
end function
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
Dim Url,Html
Url=""
Html = getHTTPPage(Url)
dim filename,MDBpath,fso,fout
filename="index.htm"
MDBpath="/"
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(server.mappath(""&filename&""))
fout.Write html
fout.close
set fout=nothing
set fso=nothing
if err then
response.Write("生成首页失败")
else
response.Write("生成首页成功")
end if
%>
7:asp站内搜索代码
body区:
<script language= "JavaScript ">
<!--
// 下面是负责搜索的页面的全路径或相对路径
var search_htm_url = "searchsource.htm ";
function searchPage() {
if ((document.searchpage.keyword.value.length == 0)
|| (document.searchpage.keyword.value == " ")) {
alert( "请填入关键字! ");
} else {
sel = document.searchpage.and_or.selectedIndex;
location.href = search_htm_url + "? "
+ escape(document.searchpage.keyword.value)
+ (sel==0? "&and ":(sel==2? "&exact ": "&or "));
}
return false;
}
document.write( ' <form name= "searchpage " onSubmit= "return searchPage() "> '
+ '请填入关键字: <input type= "text " size=22 name= "keyword "> '
+ ' <input type= "button " value= "Search " onClick= "searchPage() "> <br> '
+ ' <select name= "and_or " size=1> <option> 关键字与搜索 <option> 关键字或搜索 '
+ ' <option> 关键字异或搜索 </select> </form> ');
// -->
</script>
8:判断ASP页装载完毕
<script>
window.onload = function()
{
alert( "Done ");
}
</script>