ASP与数据库,有用的代码(转贴,摘贴)

ASP与数据库

 ASP与数据库运用:密码验证 
        Microsoft 的大作ASP(Active Server 
      Pages)以其易学易用、扩充性好、功能多而强等优点正掀起一场新的web编程革命(从严格意义上讲,编写asp并不是编程),它以令人吃惊的发展和普及速度大有取代由perl等语言编写的CGI(Common 
      Gateway Interface,通用网关接口) 的势头。基于web 
      page方式的web管理模式已经成为潮流,看看现在的网管们,有谁不会asp的编写呢?要管理?那你可能就要用到我这里要说的“密码验证”了。简单地说,密码验证就是首先判断你是不是有登录权限,如果有,就继续,否则,哼哼……。什么?你到现在还不知道ASP是什么东东?“该程序执行了非法操作,即将被关闭。如仍有问题,请与程序供应商联系。”----------系统语
        下面,我们就来看看实现密码验证的ASP需要些什么吧。
        一、 ASP运行环境:
        Windows 95/98单机平台:PWS (Personal Web Server)4.0 、windows NT 
      4.0/5.0服务器平台:IIS(Internet Information Server )Service Pack 3 及其以上版本)
        NT workstation 4.0 工作站平台:PWS(Personal Web Server )NT 
      workstation版及最新版的IE浏览器。
        二、 用于制作ASP的软件
        Windows FrontPage 98/2000 、Dreamweaver 3.0 ,如果这些软件你都没有,那你就用windows 
      中的Notepad 
      当一次“代码编写狂”吧。不过ASP中很多代码仍是需要我们手工编写大量代码的,用专用的网页制作软件只不过是偷一丁点懒而已。
        三、 用哪一种数据库作为储存用户资料(用户名及密码)的数据库呢?
        SQL Server、Microsoft Access 
      97/2000等都可以。本人建议你使用Access,因为你可能对它比较熟悉,一旦有问题,解决起来比较容易,更深的原因是:Microsoft 
      Access相对于其它非服务器等级的数据库执行的效率要高得多。
        好了,废话说了这么多,可能你早已经不耐烦了。不过,这对于一些ASP的初学者可能还是有帮助的,对于这部分读者,你们可能还得要看看关于ASP方面的书籍或网站来增加你对ASP基本语法的了解。
        让我们一步一步来做这个密码验证吧,我采用的是Windows 98 + PWS 4.0平台,IE 
      5.0浏览器,网页制作软件:FrontPage 2000. Go!
        一、创建用户密码数据库
        先用Access建立一个用户密码数据库,建立字段名id和psd,并添加值.如:id的值我设为:admin,psd的值为:www,当然,你还可以继续添加用户id及psd,完成后保存为:psd.mdb。
        二、编写psd.asp(用户登录界面页,完成验证的功臣就是它了)及log.asp(成功登录后显示的页面)。在编写之前,我们来分析一下常见的用户登录界面,比如说你想收取基于web 
      page方式免费邮件箱的登录界面:管理用户登录的文件名常常为log.*,开始登录时是这个文件,登录完成后浏览器的地址栏中还是显示的这个文件名,这是怎么回事儿呢?用ASP的方法来讲,原来,用户登录的文件被包含在登录完成后的文件中。以我现在要讲的这个例子来说,psd.asp就是被包含在log.asp中了。用户登录时看到的文件名将是:log.asp,而log.asp要求系统先执行psd.asp,通过验证之后才看到真正的log.asp网页。对了!实际上密码验证的关键在psd.asp。在你读完本文后,你会深深体会这一点。既然psd.asp文件是关键,那我们就先来看看psd.asp是怎么写的。
        运行FrontPage新建一个文件,并保存为:psd.asp(在FrontPage 的保存类型中选取“Active Server 
      Pages”)。在FrontPage 
      左下角选取“HTML”先在它的顶部进行ASP源代码的编写,内容如下(以下源代码中凡出现“‘……”的均为注释): 
        <%
        function checkPwd(id,psd) '检测用户id及密码
        dim conn,param,rs 
        set conn=server.createobject("adodb.connection") '创建数据库连接对象conn
        param="driver={microsoft access driver (*.mdb)}" 
      ‘指定数据库驱动程序,不可省略写为“access diver(*.mdb)”
        conn.open param & ";dbq=" & server.mappath("psd.mdb") 
      '用指定的数据库驱动程序打开数据库,并指定数据路径
        sql="select*from psd where id='" & id & "' and psd='" & psd & "'" 
      ‘定义sql从数据库中读取id及psd的值,本行中的第一个psd是指数据库名,以后的psd是指psd.mdb中的psd字段。
        set rs=conn.execute(sql) '打开数据库
        if rs.eof then 
        checkpwd=false 
        else 
        checkpwd=true 
        end if 
        end function 
      ‘以上几句判断是否已经读完数据库中的记录,如果没有,就向后读,如果已经完成,则验证用户名及密码。如果验证通过,则为true,反之为flase
        %> 
        <% 
        if isEmpty(session("passed")) then session("passed")=false 
      '判断用户输入信息 
        id=request("id") ‘获取用户id(用户名)
        psd=request("psd") ‘获取用户psd(密码)
        if id="" or psd="" then 
        response.write"请输入您的登录名及密码。" '如果用户没有输入完整的信息,返回出错信息。 
        elseif not checkpwd(id,psd) then 
        response.write"用户名或密码错误!
请检查你的用户名及密码然后再试一次!" 
      ‘如果用户已经输入完整信息,但输入错误也返回出错信息。
        else session("passed")=true 
        end if
        if not session("passed") then%> 
      ‘用户输入的信息完全正确并验证通过,以下开始编写html代码,做一个用户登录界面。
        
        
              charset=gb2312">
        请您输入您的用户名及密码!
        
        
          
         

 
              size="6">用户登录首页

 
         

 
              action="<%=request.serverVariables("psd.mdb")%>"> 
         
         
        用户名: 
              value="<%=id%>"> 
         
         
         密 码: 
              value="<%=psd%>"> 
         
         
          
          
         
         
              type="reset" value="清除" name="B1">

 
         
        <%response.end 
        end if %> ‘验证过程结束,进入加密网页。
         
         
        完成了psd.asp的编写,可能你早已经迫不及待地想知道log.asp怎么编写了吧。让我们继续吧!
        Log.asp的内容:
         
      ‘在log.asp源代码中的顶部输入这句,作用就是在系统执行log.asp之前先执行psd.asp啦!
        
        
        用户验证通过,您已经成功登录系统
        
        

用户验证通过,您已经成功登录!

        现在你可以进行你想要的操作了。如果你有什么问题,请来信Email      href="mailto:[email protected]?subject=问你几个关于密码验证的问题">[email protected]


        
        
        呵呵……手写了这么多,还受得了吧。你在编写完成之后,可以移植到其它平台上,比如Windows 
      NT。最后,将你做的这两个asp文件及psd.mdb数据库放在同一个目录下(该目录必须在PWS或IIS中有www服务),比如:c:\inetpub\wwwroot\。然后,在确保你的PWS或IIS在运行的情况下,在你们IE浏览器的地址栏中输入http://127.0.0.1/log.asp,看看,会有什么!(如果你在登录过程中听到你的硬盘在响,可别吓着了哟!)

 


q 文 章 点 评       3 篇


>>上篇文章:ASP技术访问WEB数据库  
>>下篇文章:制作有管理功能的ASP留言板  



 

第三招:控制你的弹出窗口只弹出一次(如果每进一次,刷新一次就弹出你不觉得很烦和麻烦吗?)有什么好的办法吗?
那是当然的啊,我们现在只要使用cookie来控制就能实现这样的要求了。
首先,你需把将如下代码加入到页面HTML的<HEAD>和</HEAD>之间:

<script>
.function openwin(){
.window.open("pop1.html","","width=120,height=240")
.}
.function get_cookie(Name) {
.var search = Name + "="
.var returnvalue = "";
.if (document.cookie.length > 0) {
.offset = document.cookie.indexOf(search)
.if (offset != -1) {
.offset += search.length
.end = document.cookie.indexOf(";", offset);
.if (end == -1)
.end = document.cookie.length;
.returnvalue=unescape(document.cookie.substring(offset, end))
. }
. }
.return returnvalue;
. }
.function loadpopup(){ //*控制弹出窗口的函数哟,你要使用他的啊
.if (get_cookie('popped')==''){
.openwin()
.document.cookie="popped=yes"
. }
.}
.//-->
</script>

 然后,用<body onload="loadpopup()">替换页面中原来的<BODY>这一句就行的了。


》》》》》》》》》》》》》》》》》》》》》》----------------------------------
在提交帖之后:
<%
if Response.Cookies("time")<>"" then
  if DateDiff('s',Response.Cookies("time"),now())<20   '隔20s才能再发帖
    Response.Write ""
    response.End
  end if
end if
Response.Cookies("time")=now()
……             '将帖子入库
----------------------------------------------------------

                    ASP项目中的公共翻页模块:

在大型的ASP项目中,很多的页面都涉及到翻页功能。如果每个页面都写一个翻页的程序的话,这样的工作即降低了工作效率,也不利于工程的模块化,不能使代码重用。因此,把翻页这样的功能模块化是很有必要的。
设计方法: 
1、调用该模块时,只需要传递记录集和每页显示的记录的条数; 
2、可以点击链接进行翻页,也可以直接输入页码,回车后翻页; 
3、不要考虑文件名,程序的每次翻页都能在当前页面。 
想清楚了上面3个问题,我们的公共翻页模块就可以动手了。 
<% 
'+++++++++++++++++++++++++++++++++++++ 
'◆模块名称: 公共翻页模块 
'◆文 件 名: TurnPage.asp 
'◆传入参数: Rs_tmp (记录集), PageSize (每页显示的记录条数) 
'◆输 出: 记录集翻页显示功能 
'+++++++++++++++++++++++++++++++++++++ 

Sub TurnPage(ByRef Rs_tmp,PageSize) 'Rs_tmp 记录集  PageSize 每页显示的记录条数; 
Dim TotalPage '总页数 
Dim PageNo '当前显示的是第几页 
Dim RecordCount '总记录条数 
Rs_tmp.PageSize = PageSize 
RecordCount = Rs_tmp.RecordCount 
TotalPage = INT(RecordCount / PageSize * -1)*-1 
PageNo = Request.QueryString ("PageNo") 
'直接输入页数跳转; 
If Request.Form("PageNo")<>"" Then PageNo = Request.Form("PageNo") 
'如果没有选择第几页,则默认显示第一页; 
If PageNo = "" then PageNo = 1 
If RecordCount <> 0 then 
Rs_tmp.AbsolutePage = PageNo 
End If 

'获取当前文件名,使得每次翻页都在当前页面进行; 
Dim fileName,postion 
fileName = Request.ServerVariables("script_name") 
postion = InstrRev(fileName,"/")+1 
'取得当前的文件名称,使翻页的链接指向当前文件; 
fileName = Mid(fileName,postion) 
%> 
 
 
 总页数:<%=TotalPage%>页 
  当前第<%=PageNo%>页 
 
<%If RecordCount = 0 or TotalPage = 1 Then 
Response.Write "首页|前页|后页|末页" 
Else%> 
?PageNo=1">首页| 
<%If PageNo - 1 = 0 Then 
Response.Write "前页|" 
Else%> 
?PageNo=<%=PageNo-1%>">前页| 
<%End If 

If PageNo+1 > TotalPage Then 
Response.Write "后页|" 
Else%> 
?PageNo=<%=PageNo+1%>">后页| 
<%End If%> 

?PageNo=<%=TotalPage%>">末页 
<%End If%> 
转到第 
<%If TotalPage = 1 Then%> 
 
<%Else%> 
 
<%End If%>页 
 
 
 
<%End Sub%> 

当然,大家可以把翻页的链接做成图片按钮,这样的话也面就更加美观了。 

调用方法: 
1、在程序开始或要使用翻页的地方包含翻页模块文件; 
2、定义变量:RowCount,每页显示的记录条数 
3、调用翻页过程:Call TurnPage(记录集,RowCount) 
4、在Do While 循环输出记录集的条件中加上" RowCount > 0 " 条件 
5、在循环结束 "Loop前" 加上: RowCount = RowCount - 1 

'----------------------------------------------------- 
调用范例: 
文件名:News.asp 

<% 
Dim Conn,Rs_News 
Set Conn = server.CreateObject("ADODB.CONNECTION") 
Conn.Open "cpm","cpm","cpm" 

Dim Sql 
Sql = "Select * from News" 
Set Rs_News = Server.CreateObject("ADODB.RECORDSET") 
Rs_News.Open Sql,Conn,1,3 '获取的记录集 

'公共翻页模块开始%> 
 
<% 
Dim RowCount 
RowCount = 10 '每页显示的记录条数 
Call TurnPage(Rs_News,RowCount) 
'公共翻页模块结束%> 

 
 
新闻编号 
新闻标题 
发布日期 
 
<% 
If Not Rs_News.eof 
Do while Not Rs_News.eof and RowCount>0 
%> 
 
<%=Rs_News("ID")%> 
<%=Rs_News("Name")%> 
<%=Rs_News("Date")%> 
 
<% 
RowCount = RowCount - 1 
Rs_News.MoveNext 
Loop 
End If 
%>  
(出处:www.dev-club.com)

 
 --------------------------------------------------------------------
 在提交帖之后:
<%
if Response.Cookies("time")<>"" then
  if DateDiff('s',Response.Cookies("time"),now())<20   '隔20s才能再发帖
    Response.Write ""
    response.End
  end if
end if
Response.Cookies("time")=now()
……             '将帖子入库


---------------------------------
怎样去除执行window.close()后弹出的‘是否关闭窗口'对话框

self.opener=null;
self.close();
但是只能在IE5.5  或IE6.0上用

最小化、最大化、关闭窗口
 

 



-------------------------------------------------------
我的下拉框中又两个选项,当选择第一个时,出现一个用户输入页面,但是有一部分input框是不要用户填写的。当选择第二项时,出现地页面是全部都要填写的。不知道该如何实现?





------------------------------------------------------------------>>>>>>>>>>>>>>
我的目的是:打开一个web页(就称为'原页' 吧),点击一个按钮弹出一个小窗口(原页不关,小窗口最好可以拖动),在小窗口里提交一个表单到原页。有什么方法可以实现?
给『原页』一个name属性,表单的target指向它


<<<<<<<<<<<<<<<<<<<<<<<==============================================
没有边框的窗口::;;;;




Chromeless  Window


/*---  Special  Thanks  For  andot  ---*/

/*
  This  following  code  are  designed  and  writen  by  Windy_sk  
  You  can  use  it  freely,  but  u  must  held  all  the  copyright  items!
*/

/*---  Thanks  For  andot  Again  ---*/

var  CW_width =  400;
var  CW_height =  300;
var  CW_top =  100;
var  CW_left =  100;
var  CW_url =  "http://www.cnbruce.com/bluebook/";
var  New_CW =  window.createPopup();
var  CW_Body =  New_CW.document.body;
var  content =  "";
var  CSStext =  "margin:1px;color:black;  border:2px  outset;border-style:expression(onmouseout=onmouseup=function(){this.style.borderStyle='outset'},  onmousedown=function(){if(event.button!=2)this.style.borderStyle='inset'});background-color:buttonface;width:16px;height:14px;font-size:12px;line-height:11px;cursor:Default;";

//Build  Window
include.startDownload(CW_url,  function(source){content=source});

function  insert_content(){
 var  temp  =  "";
 CW_Body.style.overflow  =  "hidden";
 CW_Body.style.backgroundColor =  "white";
 CW_Body.style.border  =    "solid  black  1px";
 content  =  content.replace(/]*)>/g,"");
 temp  +=  "";
 temp  +=  "";
 temp  +=  "Chromeless  Window  For  IE6  SP1";
 temp  +=  "";
 temp  +=  "?";
 temp  +=  "0";
 temp  +=  "1";
 temp  +=  "x";
 temp  +=  "";
 temp  +=  "";
 temp  +=  content;
 temp  +=  "
";
 temp  +=  "";
 CW_Body.innerHTML  =  temp;
}

setTimeout("insert_content()",1000);

var  if_max  =  true;
function  show_CW(){
 window.moveTo(10000,  10000);
 if(if_max){
  New_CW.show(CW_top,  CW_left,  CW_width,  CW_height);
  if(typeof(New_CW.document.all.include)!="undefined"){
   New_CW.document.all.include.style.width  =  CW_width;
   New_CW.document.all.Max.innerText  =  "1";
  }

 }else{
  New_CW.show(0,  0,  screen.width,  screen.height);
  New_CW.document.all.include.style.width  =  screen.width;
 }
}

window.onfocus    =  show_CW;
window.onresize  =  show_CW;

//  Move  Window
var  drag_x,drag_y,draging=false

function  drag_move(e){
 if  (draging){
  New_CW.show(e.screenX-drag_x,  e.screenY-drag_y,  CW_width,  CW_height);
  return  false;
 }
}

function  drag_down(e){
 if(e.button==2)return;
 if(New_CW.document.body.offsetWidth==screen.width  &&  New_CW.document.body.offsetHeight==screen.height)return;
 drag_x=e.clientX;
 drag_y=e.clientY;
 draging=true;
 e.srcElement.setCapture();
}

function  drag_up(e){
 draging=false;
 e.srcElement.releaseCapture();
 if(New_CW.document.body.offsetWidth==screen.width  &&  New_CW.document.body.offsetHeight==screen.height)  return;
 CW_top    =  e.screenX-drag_x;
 CW_left  =  e.screenY-drag_y;
}



============================================================>>>>>>>>>>>>>>>>>>>>>


还有图片的“黑白转彩色”


onmouseleave='doTrans("gray")'  
style="FILTER:  progid:DXImageTransform.Microsoft.Fade(Overlap=1.00);  WIDTH:  1px"  
onmouseenter='doTrans("")'>
  

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
模拟office菜单:::
:::::::::::::

*  {  font-size:  12px;  }
body  {  margin:  0px;  }



//  Office  XP  菜单

var  sub_display  =  false;

//  颜色数组说明:此数组储存菜单各部份颜色样式,可以改变颜色值达到改变样式的效果
//  值依次为:高亮背景色,  高亮边框色,  菜单栏背景色,  子菜单背景色,  子菜单边框色,  子菜单标题色,  子菜单阴影色

var  color  =  ['#B6BDD2',  '#0A246A',  '#D4D0C8',  '#F8F8F8',  '#666666',  '#DBD8D1',  '#DDDDDD'];

//  菜单数组说明:此数组储存各菜单数据
//  值依次为:
//  1.  主菜单名称,  下拉菜单右延空白长度
//  2.  第1个子菜单名称,  链接地址
//  3.  第2个子菜单名称,  链接地址
//  4.  ......

var  menu  =  new  Array();
menu[0]  =  [['菜单一',  50],  ['1111',  '1.htm'],  ['2222',  '2.htm'],  ['3333',  '3.htm']];
menu[1]  =  [['菜单二',  50],  ['1111',  '1.htm'],  ['2222',  '2.htm'],  ['3333',  '3.htm']];
menu[2]  =  [['菜单三',  50],  ['1111',  '1.htm'],  ['2222',  '2.htm'],  ['3333',  '3.htm']];
menu[3]  =  [['菜单四',  50],  ['1111',  '1.htm'],  ['2222',  '2.htm'],  ['3333',  '3.htm']];
menu[4]  =  [['菜单五',  50],  ['1111',  '1.htm'],  ['2222',  '2.htm'],  ['3333',  '3.htm']];
menu[5]  =  [['菜单六',  50],  ['1111',  '1.htm'],  ['2222',  '2.htm'],  ['3333',  '3.htm']];


document.write('');
for  (var  i=0;  idocument.write(''  +  menu[i][0][0]  +  '');
document.write('');

for  (var  i=0;  i        document.write('
'  +  menu[i][0][0]  +  '
');
        for  (var  j=1;  j        document.write(' '  +  menu[i][j][0]  +  '');
        document.write('');
}

function  Menu_Over(obj,  s)  {
        if  (sub_display)  {
                subMenu_Show(obj,  s)
        }
        else  {
                obj.style.backgroundColor  =  color[0];
                obj.style.border  =  '1px  '  +  color[1]  +  '  solid';
        }
}

function  Menu_Out(obj)  {
        obj.style.backgroundColor  =  '';
        obj.style.border  =  '1px  '  +  color[2]  +  '  solid';
}

function  Menu_Click(obj,  s)  {
        subMenu_Show(obj,  s)
}

function  subMenu_Over(obj)  {
        obj.style.backgroundColor  =  color[0];
        obj.style.border  =  '1px  '  +  color[1]  +  '  solid';
}

function  subMenu_Out(obj)  {
        obj.style.backgroundColor  =  '';
        obj.style.border  =  '1px  '  +  color[3]  +  '  solid';
}

function  subMenu_Hide(hide)  {
        for  (var  i=0;  i        subMenu[i].style.display  =  'none';
        sub_display  =  hide;
}

function  subMenu_Show(obj,  s)  {
        subMenu_Hide(false);
        subMenu(s).style.posLeft  =  obj.offsetLeft  +  6;
        subMenu(s).style.display  =  '';
        sub_display  =  true;
}

window.onfocus  =  subMenu_Hide;
  
=-=======================-----------------========================================

 

模仿OUTLOOK的菜单:


.titleStyle{
background-color:#3366cc;color:#ffffff;border-top:1px  solid  #FFFFFF;font-size:9pt;cursor:hand;
}
.contentStyle{
background-color:#efefef;color:blue;font-size:9pt;
}

a{
color:blue;
}
body{
font-size:9pt;
}








你可能感兴趣的:(ASP与数据库,有用的代码(转贴,摘贴))