欢迎光临
我们一直在努力

代码例子 – Ask For Login

建站超值云服务器,限时71元/月

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.

lets 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, lets 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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 代码例子 – Ask For Login
分享到: 更多 (0)