动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Asp → 阅读文章

 代码例子 - Ask For Login

作者来源: 
阅读 4220 人次 , 2000-12-3 

The need for a security system is obvious if your pages include sensitive information.

This sample shows you how to setup some of your pages to ask for login if the user has not logged in yet.

2eNetWorX/dev site uses this kind of protection for interactive pages and file downloads.

Let's assume that we have a page where the visitors can ask questions. We want to make sure that the user
has logged in before being able to ask a question.

If the user has not logged in yet, we will ask for a login. After a successful login, we will redirect the
user to the page requested.

First, we will have a common file to check for login. You need to include this file into all the pages
that require login.

security.asp

<%

bLoggedIn = (len(session("UserName")) > 0)

if bRequireLogin then
'Login required
if Not bLoggedIn then
'Not logged in, ask for login
response.redirect "login.asp?comebackto=" & _
request.servervariables("script_name") & "?" & _
server.urlencode(request.querystring)
'Note how we construct the page to come back
end if
end if

%>

login.asp

First thing to do in our login.asp page is to get the page where the user is redirected from. The variable
sReferer is used to redirect back to the page the user has come from.

sGoBackTo variable will used in the <form> tag for persisting this location when we submit the login form.

<%

if request("comebackto") <> "" then
sReferer = request("comebackto")
sGoBackTo = "?" & request.querystring
end if

if request("cmdLogin") <> "" then

'Login Form submitted
sUserName = request("txtUserName")
sPassword = request("txtPassword")

'Check for username and password

if sUserName = "bill" And sPassword = "gates" then
bLoginSuccessful = True
end if

session("UserName") = sUserName


'After a successful login, let's send the user
'back to the page requested. The variable sReferer
'holds the page to go back, if it is empty, we should
'redirect the user to our default page.

if sReferer = "" then
response.redirect "index.asp"
else
response.redirect sReferer
end if

else

'Display the Login Form

%>

<form action="login.asp<%=sGoBackTo%>" method="post">
<input type="text" name="txtUserName"><br>
<input type="password" name="txtPassword"><br>
<input type="submit" name="cmdLogin"><br>
</form>

<%

end if

%>

testpage.asp

Now, all you need to do is setting bRequireLogin to True and including the security.asp file.

<%

bRequireLogin = True

%>

<!--#include file="security.asp"-->

Users should be logged in to see this text.

I hope this sample gives you a quick overview of a simple security system implementation with ASP.

You are always welcome to express your feedback!

by Hakan Eskici
  

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:在Javascript中为String对象添加trim,ltrim,rtrim方法
· 下一篇:浏览器语种检测,适合于多语言版本的站点
· 排序方式解决“上下主题”问题(一)
· 获得上一个月几月
· 利用ASP实现三个强大功能(二) (转,强烈推荐)
· 服务器获得客户端时间的方法
· Flash和Asp数据库的结合应用


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077