XML Login

A drawback of the standard ASP login form is that one has to submit the form in order to process the users information by checking it against records stored in a database. It would be quite handy if the user was alerted immediately after clicking the login button.

This can be done by using javascript and ActiveXObjects. Unfortunately, it's only possible to use the MSXML.DOMDocument object in IE5 and above. So, this methodolgy is only really suitable for Extranets or Intranets.

The logic involves loading XML from an ASP page. We can evaluate the result and reacr accordingly. On login.asp, we'll have our javascript function in the head of the document:

<script language="JavaScript">
<!--
function cusLogin() {
var objDOM=new ActiveXObject("MSXML.DOMDocument");
objDOM.async=false;
var theForm=document.form1
var theTarget="xml_login.asp?UserName=" + theForm.UserName.value
theTarget += "&UserPassword=" + theForm.UserPassword.value
objDOM.load(theTarget);
var theResponse=objDOM.documentElement.selectNodes("Row")[0].text
if (theResponse == 'false') {
alert('Sorry, your login failed');
} else {
alert('Your login succeeded, you\'ll be taken into site');
document.location.href='login_success.asp'
}
}
//-->
</script>

and our HTML form:

<form name="form1" method="post" action="">
<table border="0" cellspacing="1" cellpadding="4" align="center">
<tr>
<td width="100" >&nbsp;</td>
<td>Login</td>
</tr>
<tr>
<td align="right" ><b>User Name:</b></td>
<td> <input name="UserName" type="text" id="UserName" size="20"> </td>
</tr>
<tr>
<td align="right" ><b>Password:</b></td>
<td>
<input name="UserPassword" type="password" id="UserPassword" size="20">
</td>
</tr>
<tr>
<td >&nbsp;</td>
<td><input type="button" value="Login" onClick="cusLogin()" name="Button">
</td>
</tr>
</table>
</form>

Next, we create our xml_login.asp (code below) page. I hope you can see the logic of this. If the username and password is incorrect then the Row node text will contain false else a session variable is set and the Row node has a value of true. Our javascript function then evaluates the XML and displays an alert accordingly.

<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../Connections/site.asp" -->
<%
UserName=Request.QueryString("UserName")
UserPassword=Request.QueryString("UserPassword")
%>
<%
Dim Recordset1__var1
Recordset1__var1 = "0"
If (UserName <> "") Then
Recordset1__var1 = UserName
End If
%>
<%
Dim Recordset1__var2
Recordset1__var2 = "0"
If (UserPassword <> "") Then
Recordset1__var2 = UserPassword
End If
%>
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_site_STRING
Recordset1.Source = "SELECT UserID, AdminUsername, AdminPassword FROM Admin_Users WHERE AdminUsername ='" + Replace(Recordset1__var1, "'", "''") + "' AND AdminPassword = '" + Replace(Recordset1__var2, "'", "''") + "'"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>
<%
if not Recordset1.eof then
varLogin="true"
Session("UserID")=Recordset1("UserID")
else
varLogin="false"
end if

Dim sXML
sXML = "<Root>" & VbCrLf
sXML = sXML & "<Row>" & VbCrLf
sXML = sXML & varLogin & VbCrLf
sXML = sXML & "</Row>" & VbCrLf
sXML = sXML & "</Root>"
Response.ContentType = "text/xml"
Response.Write sXML
%>
<%
Recordset1.Close()
Set Recordset1 = Nothing
%>

AddThis Social Bookmark Button

Related Links

Extensions About me Articles Downloads Portfolio

Products

Charon Cart 3 Extension Charon Cart.NET v2 Multi-lingual Application Charon Cart 2 Extension