IDパスワード認証サンプル(データベース)


<% @LANGUAGE="VBScript" %>
<%
'--------------------------------------A
if Request.Form = "" then
%>
 
<html>
<head>
<title>Login</title>
</head>
<body>
<div align="center">【ID認証】 </div>
<form method="POST" action="login.asp">
<table align="center">
<tr>
<td>ID</td>
<td><input type="text" name="userid"></td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" value="OK">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
 
<%
'--------------------------------------B
else
 
userid = Request.Form ("userid")
password = Request.Form ("password")
 
Set db = Server.CreateObject("ADODB.Connection")
db.Open "Table","ID","Password"
 
strSQL ="select * from Table where id = '" & userid & "' and pass = '" & password & "'"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, db, 1, 1
 
if rs.eof or rs.bof then
 Response.Write "IDかパスワードが違います"
else
 Response.Write "認証しました"
end if
 
rs.Close
set rs = nothing
db.Close
set db = nothing
 
end if
 
%>
 

--A の部分が認証前(ID、パスワードを入力して、ボタンを押す前)のHTML表示部分。

--B の部分からが、IDパスワードを入力して、送信ボタンを押した後の処理。

上のサンプルでは、IDパスワードがあっている場合、「認証しました」と画面に表示して、間違っていた場合、「IDかパスワードが違います」と出している。