ASP User Authentication/Login From Database |
ASP User Authentication From Database ตัวอย่างการใช้งาน Authentication ตรวจสอบค่า User และ Password จากฐานข้อมูล Microsoft Access
Syntax
Response.Status = "401 Unauthorized"
Response.AddHeader "WWW-Authenticate","Basic Realm=""localhost"""
AspAuthDatabaseCheck.asp
<%
'*** Function Decode Authentication Code ***'
Function Decode(strCode)
Dim UUEncode,i,OffSet,numBytes,byteGroup,groupBytes
Dim CharCounter,thisChar,thisByte,k
Set UUEncode = Server.CreateObject("Scripting.Dictionary")
For i=0 To 63
Select Case i
Case 0 OffSet = 65
Case 26 OffSet = 71
Case 52 OffSet = -4
End Select
UUEncode(Chr(i+OffSet)) = i
Next
For byteGroup = 1 To Len(strCode) Step 4
numBytes = 3
groupBytes = 0
For CharCounter = 0 to 3
thisChar = Mid(strCode,byteGroup+CharCounter,1)
If thisChar = "=" Then
numBytes = numBytes - 1
thisByte = 0
Else
thisByte = UUEncode(thisChar)
End If
groupBytes = 64*groupBytes+thisByte
Next
For k = 1 To numBytes
Select Case k
Case 1: thisChar = groupBytes \ 65536
Case 2: thisChar = (groupBytes And 65535) \ 256
Case 3: thisChar = (groupBytes And 255)
End Select
Decode = Decode & Chr( thisChar )
Next
Next
End Function
'*** End Function Decode Authentication Code ***'
Dim strAuth,authSplit,strUser,strPassword
Sub subAuthentication()
Response.Clear()
Response.Status = "401 Unauthorized"
Response.AddHeader "WWW-Authenticate","Basic Realm=""localhost"""
Response.end
End Sub
IF Trim(Request.ServerVariables("HTTP_AUTHORIZATION")) <> "" Then
strAuth = Request.ServerVariables("HTTP_AUTHORIZATION")
strAuth = Trim(Mid(strAuth,6))
strAuth = Decode(strAuth)
authSplit = Split(strAuth,":")
strUser = authSplit(0)
strPassword = authSplit(1)
If Trim(strUser) = "" Or Trim(strPassword) = "" Then
Call subAuthentication()
Else
'*** Check From Database ***'
Dim Conn,strSQL,objRec
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("mydatabase.mdb"),"" , ""
strSQL = "SELECT * FROM user WHERE User = '"&strUser&"' AND Password = '"&strPassword&"' "
Set objRec = Conn.Execute(strSQL)
If objRec.EOF Then
Call subAuthentication()
Else
Session("strUser") = strUser
Session("strPassword") = strPassword
End If
objRec.Close()
Conn.Close()
Set objRec = Nothing
Set Conn = Nothing
End If
If Session("strUser") = "" Or Session("strPassword") = "" Then
Call subAuthentication()
End IF
%>
AspAuthenticationDatabase1.asp
<!--#include file="AspAuthCheck.asp"-->
<html>
<head>
<title>ThaiCreate.Com ASP & Authentication</title>
</head>
<body>
<%
Response.write("<center><h1>Welcome "&Session("strUser")&" </h1><br><br><h2>Now Page 1</h2><center>")
%>
<center><a href="AspAuthenticationDatabase2.asp">Page 2</a><center>
</body>
</html>
AspAuthenticationDatabase2.asp
<!--#include file="AspAuthCheck.asp"-->
<html>
<head>
<title>ThaiCreate.Com ASP & Authentication</title>
</head>
<body>
<%
Response.write("<center><h1>Welcome "&Session("strUser")&" </h1><br><br><h2>Now Page 2</h2><center>")
%>
<center><a href="AspAuthenticationDatabase1.asp">Page 1</a><center>
</body>
</html>
คำอธิบาย
จากตัวอย่างถ้าผู้ใช้เปิดไฟล์ AspAuthenticationDatabase1.asp หรือ AspAuthenticationDatabase2.asp ถ้ายังไม่ผ่านการ Login โปรแกรมจะให้ทำการกรอก User/Password โดยนำข้อมูลไปเช็คใน MySQL ว่ามี User และ Password หรือไม่ ถ้ามีให้ทำการบันทึกเป็นค่า Session เพื่อนำไปใช้งานต่อไป
Screenshot
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-10-30 23:46:08 /
2010-11-08 12:14:03 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|