PDA

View Full Version : Trouble with register script


Crash1hd
05-14-2003, 07:45 AM
Ok the following script has been edited majorly from the original not that there was anything wrong with the original I just didnt want to use cookies! anyhow I am having trouble with the code below is giving me the following error

error '80020009'
Exception occurred.

/Login/register.asp, line 172

Any thoughts?

<% @Language="VBScript" %>
<% Option Explicit %>
<!--#INCLUDE Virtual="/Include/Header.asp"-->

<%
'*************************************** FUNCTIONS

Function SQLFormat(byVal str)
If IsNull(str) Then str = ""
SQLFormat = Replace(str,"'","''")
End Function

Function ExtractNumbers(byVal str)
If IsNull(str) Then str = ""
Dim enRegEx
Set enRegEx = New RegExp
enRegEx.Pattern = "\D"
enRegEx.Global = True
ExtractNumbers = enRegEx.Replace(str,"")
End Function

Function RemoveExtraSpaces(byVal str)
If IsNull(str) Then str = ""
Dim resRegEx
Set resRegEx = New RegExp
resRegEx.Pattern = "\s+"
resRegEx.Global = True
RemoveExtraSpaces = resRegEx.Replace(str," ")
End Function

Function ValidEmail(str)
Dim veRegEx
Set veRegEx = New RegExp
veRegEx.Pattern = "^[^@\s]+@[^@\s]+\.[a-zA-Z0-9]{2,}$"
ValidEmail = veRegEx.Test(str)
End Function

Function RequestFormat(str)
If IsNull(str) Then Exit Function
RequestFormat = Trim(RemoveExtraSpaces(Replace(str,vbTab,"")))
End Function

'***************************** DIMENSION VARIABLES

' These variables are being dimmed now since they may be used many places.
' Some others, such as database queries, will be dimmed in the
' subroutine in which they are used.

' Connection variables
Dim Conn, rs, sConnString, sMapPath

Dim emc ' Email confirmation variable (email address from querystring)
Dim uid
Dim emailfound ' This will be used for registration confirmation

' Variables used on the member registration form
Dim First_Name
Dim Last_Name
Dim Address
Dim City
Dim ProvinceField
Dim PostalCodeField
Dim Day_Phone_A
Dim Day_Phone_B
Dim Day_Phone_C
Dim Night_Phone_A
Dim Night_Phone_B
Dim Night_Phone_C
Dim username ' user is a reserved word in Access
Dim pass ' password is a reserved word in Access
Dim email
Dim userid ' We'll use this to confirm their account

Dim submitnumber ' This is used to determine whether the form has been submitted

Dim usernametaken ' This will be used to determine whether the username is in use
Dim emailaddresstaken ' This will be used to determine whether the email address is taken


'******************************* REQUEST VARIABLES

Select Case Request.ServerVariables("REQUEST_METHOD")
Case "GET"
emc = Request.QueryString("emc")
uid = Request.QueryString("uid")
Case "POST"
First_Name = RequestFormat(Request.Form("First_Name"))
Last_Name = RequestFormat(Request.Form("Last_Name"))
Address = RequestFormat(Request.Form("Address"))
City = RequestFormat(Request.Form("City"))
ProvinceField = RequestFormat(Request.Form("ProvinceField"))
PostalCodeField = RequestFormat(Request.Form("PostalCodeField"))
Day_Phone_A = RequestFormat(Request.Form("Day_Phone_A"))
Day_Phone_B = RequestFormat(Request.Form("Day_Phone_B"))
Day_Phone_C = RequestFormat(Request.Form("Day_Phone_C"))
Night_Phone_A = RequestFormat(Request.Form("Night_Phone_A"))
Night_Phone_B = RequestFormat(Request.Form("Night_Phone_B"))
Night_Phone_C = RequestFormat(Request.Form("Night_Phone_C"))
username = RequestFormat(Request.Form("username"))
pass = RequestFormat(Request.Form("pass"))
email = RequestFormat(Request.Form("email"))
submitnumber = Request.Form("submitnumber")
End Select

'********************** INITIALIZE OTHER VARIABLES

submitnumber = submitnumber + 1
emailfound = True
usernametaken = False
emailaddresstaken = False

%>

<%
'************************************ MAIN PROGRAM

Call OpenConnection()
If emc <> "" Then
Call ConfirmRegistration()
If emailfound = False Then
Call DisplayRegistrationForm()
End If
Else
If First_Name <> "" AND _
Last_Name <> "" AND _
Address <> "" AND _
City <> "" AND _
ProvinceField <> "" AND _
PostalCodeField <> "" AND _
Day_Phone_A <> "" AND _
Day_Phone_B <> "" AND _
Day_Phone_C <> "" AND _
username <> "" AND _
pass <> "" AND _
ValidEmail(email) = True Then
Call CheckForDupes()
If usernametaken = False AND emailaddresstaken = False Then
Call InsertNewMember()
Call DisplayThankYouPage()
Else
Call DisplayRegistrationForm()
End If
Else
Call DisplayRegistrationForm()
End If
End If
Call CloseConnection()

'******************************** END MAIN PROGRAM


'************************************* SUBROUTINES

Sub OpenConnection() '''''''''''''''''''''''''''''
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Mid(Server.MapPath("\"), 1, InStrRev(Server.MapPath("\"),"\")-1) & "\AR DbFiles\AR.mdb;" & _
"Persist Security Info=False;"
Set Conn = Server.CreateObject("ADODB.Connection")
Set RS = Server.CreateObject("ADODB.Recordset")
Conn.Open sConnString
End Sub ''''''''''''''''''''''''''''''''''''''''''

Sub CloseConnection() ''''''''''''''''''''''''''''
Conn.Close
Set Conn = Nothing
End Sub ''''''''''''''''''''''''''''''''''''''''''

Sub ConfirmRegistration() ''''''''''''''''''''''''
Dim ConfirmQuery
ConfirmQuery = "SELECT * FROM members WHERE email = " & emc & " and userid = " & uid
Set rs = Conn.Execute(ConfirmQuery)
If RS.fields("confirmed") = True Then
Response.Redirect("/login.asp") ' This sends them to the login page letting it know they came from here
Else
If NOT rs.EOF Then
Dim UpdateConfirmedStatus
UpdateConfirmedStatus = "UPDATE members SET confirmed = True WHERE email = " & emc & " and userid = " & uid
Conn.Execute(UpdateConfirmedStatus)
response.write("<p align=center><b>Thankyou " & RS.fields("First_Name") & " for registering click <a href=/login.asp>here</a> to login</b></p>")
Else
emailfound = False ' The email was not found, they still need to register
End If
End If
End Sub ''''''''''''''''''''''''''''''''''''''''''

Sub CheckForDupes() ''''''''''''''''''''''''''''''
Dim DupeEmailQuery
DupeEmailQuery = "SELECT email FROM members WHERE email = '" & SQLFormat(email) & "'"
Set rs = Conn.Execute(DupeEmailQuery)
If NOT rs.EOF Then emailaddresstaken = True
Dim DupeUsernameQuery
DupeUsernameQuery = "SELECT email FROM members WHERE username = '" & SQLFormat(username) & "'"
Set rs = Conn.Execute(DupeUsernameQuery)
If NOT rs.EOF Then usernametaken = True
End Sub ''''''''''''''''''''''''''''''''''''''''''

Sub InsertNewMember() ''''''''''''''''''''''''''''
Dim InsertQuery
InsertQuery = "INSERT INTO members(username,pass,email,First_Name,Last_Name,Address,City,ProvinceField,PostalCodeField,Day_Phon e_A,Day_Phone_B,Day_Phone_C,Night_Phone_A,Night_Phone_B,Night_Phone_C,created,confirmed) VALUES('" & SQLFormat(Left(username,255)) & "','" & SQLFormat(Left(pass,255)) & "','" & SQLFormat(Left(email,255)) & "','" & SQLFormat(Left(First_Name,255)) & "','" & SQLFormat(Left(Last_Name,255)) & "','" & SQLFormat(Left(Address,255)) & "','" & SQLFormat(Left(City,255)) & "','" & SQLFormat(Left(ProvinceField,255)) & "','" & SQLFormat(Left(PostalCodeField,255)) & "','" & SQLFormat(Left(Day_Phone_A,255)) & "','" & SQLFormat(Left(Day_Phone_B,255)) & "','" & SQLFormat(Left(Day_Phone_C,255)) & "','" & SQLFormat(Left(Night_Phone_A,255)) & "','" & SQLFormat(Left(Night_Phone_B,255)) & "','" & SQLFormat(Left(Night_Phone_C,255)) & "',#" & Now() & "#,False)"
Conn.Execute(InsertQuery)
Dim GetMemberQuery
GetMemberQuery = "SELECT userid FROM members WHERE username = '" & SQLFormat(username) & "' AND pass = '" & SQLFormat(pass) & "'"
Set rs = Conn.Execute(GetMemberQuery)
If NOT rs.EOF Then'This should never be EOF...
userid = rs("userid")
End If
End Sub ''''''''''''''''''''''''''''''''''''''''''
%>
<% Sub DisplayRegistrationForm() ''''''''''''''''' %>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head><title>Please Fill In The Form Below</title>
<LINK href="/Scripts/style.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="/Scripts/Check.js"></script>
</head>
<body>
<div>
<% If emailfound = False Then %>
<p class=title>Your email address was not found in our members database.</p>
<% End If %>
<form name="Regform" method="post" action="register.asp">
<div align="Center">
<table cellspacing="2" cellpadding="5" border="0">
<tr>
<td align="right" colspan="2"> <p align="center">
<b>Please Fill In The Form Below To Register!</b></p></td>
</tr>
<tr>
<td align="right" colspan="2">
<p align="left">
<b><font face="Arial">Your Info</font></b></td>
</tr>
<tr>
<td align="right"><font color="red">First Name: </font></td>
<td><input id="First_Name" maxlength="100" size="30" name="First_Name" value="<% = Server.HTMLEncode(First_Name) %>" />
<% If submitnumber > 1 AND First_Name = "" Then Response.Write("<span style=""color:#cc0000""> * Required</span>") %></td>
</tr>
<tr>
<td align="right"><font color="red">Last Name: </font></td>
<td><input id="Last_Name" maxlength="100" size="30" name="Last_Name" value="<% = Server.HTMLEncode(Last_Name) %>" />
<% If submitnumber > 1 AND Last_Name = "" Then Response.Write("<span style=""color:#cc0000""> * Required</span>") %></td>
</tr>
<tr>
<td align="right"><font color="red">Street: </font></td>
<td><input id="Address" maxlength="100" size="30" name="Address" value="<% = Server.HTMLEncode(Address) %>" />
<% If submitnumber > 1 AND Address = "" Then Response.Write("<span style=""color:#cc0000""> * Required</span>") %></td>
</tr>
<tr>
<td align="right"><font color="red">City: </font></td>
<td><input id="City" maxlength="100" size="30" name="City" value="<% = Server.HTMLEncode(City) %>" />
<% If submitnumber > 1 AND City = "" Then Response.Write("<span style=""color:#cc0000""> * Required</span>") %></td>
</tr>
<tr>
<td align="right"><font color="red">Province: </font></td>
<td>
<select size="1" name="ProvinceField">
<option selected>Select Province</option>
<option value="BC">B.C.</option>
<option value="Alberta">Alberta</option>
<option value="Saskatchewan">Saskatchewan</option>
<option value="Manitoba">Manitoba</option>
<option value="Ontario">Ontario</option>
<option value="Quebec">Quebec</option>
<option value="Newfoundland">Newfoundland</option>
<option value="PEI">P.E.I.</option>
<option value="New Brunswick">New Brunswick</option>
<option value="Nova Scotia">Nova Scotia</option>
<option value="Northwest Territories">Northwest Territories</option>
<option value="Nunavut">Nunavut</option>
<option value="Baffin Island">Baffin Island</option>
</select>
</td>
</tr>
<tr>
<td align="right"> <font color="red">Postal Code: </font></td>
<td><input id"PostalCodefield" maxLength="7" name="PostalCodeField" size="17" value="<% = Server.HTMLEncode(PostalCodeField) %>" />
<% If submitnumber > 1 AND PostalCodeField = "" Then Response.Write("<span style=""color:#cc0000""> * Required</span>") %></td>
</tr>
<tr>
<td align="right"><font color="red">Daytime Phone #: </font></td>
<td><input id="Day_Phone_A" maxlength="3" size="3" name="Day_Phone_A"> &nbsp;<input id="Day_Phone_B" maxlength="3" size="3" name="Day_Phone_B">
&nbsp;<input id="Day_Phone_C" maxlength="4" size="4" name="Day_Phone_C"></td>
</tr>
<tr>
<td align="right"><font color="red">Night Time Phone #: </font></td>
<td><input id="Night_Phone_A" maxlength="3" size="3" name="Night_Phone_A"> &nbsp;<input id="Night_Phone_B" maxlength="3" size="3" name="Night_Phone_B">
&nbsp;<input id="Night_Phone_C" maxlength="4" size="4" name="Night_Phone_C">
<input id="Night_Phone_Same_As_Day" type="checkbox" name="Night_Phone_Same_As_Day" value="ON">
Same As Day Phone #</td>
</tr>
<tr>
<td align="right"><font color="red">Email Address: </font></td>
<td><input id="email" maxlength="100" size="30" name="email" value="<% = Server.HTMLEncode(email) %>">
<% If submitnumber > 1 AND ValidEmail(email) = False Then
Response.Write("<span style=""color:#cc0000""> * Invalid Email Address</span>")
ElseIf emailaddresstaken = True Then
Response.Write("<span style=""color:#cc0000""> * Email already in use</span>")
End If %>
</td>
</tr>
<tr>
<td align="right"><font color="red">Username: </font></td>
<td><input id="username" maxlength="100" size="30" name="username" value="<% = Server.HTMLEncode(username) %>">
<% If submitnumber > 1 AND username = "" Then
Response.Write("<span style=""color:#cc0000""> * Invalid Username</span>")
ElseIf usernametaken = True Then
Response.Write("<span style=""color:#cc0000""> * Username already in use</span>")
End If %>
</td>
</tr>
<tr>
<td align="right"><font color="#FF0000">Password: </font></td>
<td>
<input id="pass" maxlength="100" size="30" name="pass" type="password" value="<% = Server.HTMLEncode(pass) %>">
<% If submitnumber > 1 AND pass = "" Then Response.Write("<span style=""color:#cc0000""> * Required</span>") %>
</td>
</tr>
<tr>
<td align="right"><font color="#FF0000">Confirm Password: </font></td>
<td>
<input id="passchk" maxlength="100" size="30" name="passchk" type="password"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="submitnumber" value="<% = submitnumber %>" />
</td>
<tr>
<td align="center" colspan="2">
<input type="Button" value="Submit" onClick="CheckReg()">&nbsp;
<input type="reset" value="Reset">&nbsp;
<input type="Button" value="Go Back" onClick="history.go(-1)">
<br><br><u><b>All fields must be filled in</b></u><p><b>Do not use any &quot; or ' in any fields they will not work</b><br>
<b>By clicking send you are agreeing to all the terms of this site!</b></td>
</tr>
</table>
</div>
</form>
</div>
</body>
</html>
<% End Sub ''''''''''''''''''''''''''''''''''''''' %>

<% Sub DisplayThankYouPage() ''''''''''''''''''''' %>
<%
' Send the confirmation email - uncomment and customize the message below...

Dim MessageBody, objMail

MessageBody = "<LINK href='http://www.alwaysremember.ca/Scripts/style.css' rel='stylesheet' type='text/css'>"
MessageBody = MessageBody & "<p align='center'><a name='Top' href='http://www.Alwaysremember.ca'><img name='Title' src='http://www.Alwaysremember.ca/Images/title.gif' alt='Title' border='0'></a></p><br>"
MessageBody = MessageBody & "Welcome " & First_Name & " " & Last_Name & " to <a name='Top' href='http://www.Alwaysremember.ca'>Alwaysremember.ca</a><br><br>"
MessageBody = MessageBody & "<b><a href=http://www.Alwaysremember.ca/Login/register.asp?emc='" & email & "'&uid=" & userid & ">Please click here to confirm your registration and activate your account:</a></b>"
MessageBody = MessageBody & "<hr>"
MessageBody = MessageBody & "&nbsp;&nbsp;&nbsp;Username: " & username & "<br>"
MessageBody = MessageBody & "&nbsp;&nbsp;&nbsp;Password: " & pass & "&nbsp;&nbsp;&nbsp; *Don't forget your password*<br>"
MessageBody = MessageBody & "<hr>"
MessageBody = MessageBody & "Remember to never give out you password<br><br>"
MessageBody = MessageBody & "Thankyou for registering with us!<br><br>"
MessageBody = MessageBody & "Adam Kirk<br>Customer Service Manager<br><a name='Top' href='http://www.Alwaysremember.ca'>Alwaysremember.ca</a><br><br>"
MessageBody = MessageBody & "Please feel free to contact us at <A href='mailto: customerservice@alwaysremember.ca' target=_blank>customerservice@alwaysremember.ca</A>"
MessageBody = MessageBody & "<br><br><br><br><br><br><br><br><p align='center'>If you have recieved this email and you never requested this <a href=#>click here</a></P>"
MessageBody = MessageBody & "<input type='hidden' name='First_Name' value='First_Name'>"

Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.To = email
objMail.From = "registrar@alwaysremember.ca"

objMail.Subject = "Registration Email Confirmation for Alwaysremember.ca"
objMail.BodyFormat = 0
objMail.MailFormat = Request.Form("MailFormatField")
objMail.Body = MessageBody
objMail.Send
Set objMail = Nothing
%>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head><title>Thank You!</title>
<LINK href="/Scripts/style.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="/Scripts/Check.js"></script>
</head>
<body>
<div align="center">
<b>Thank you for registering!<br /><br /></b>
You will receive an email shortly asking you to confirm your registration.<br /><br />
<a href="/login.asp">Click here to login</a><br />
</div>
</body>
</html>
<% End Sub ''''''''''''''''''''''''''''''''''''''' %>

raf
05-14-2003, 08:17 AM
Well, would be nice to set the 172 line in bold :D

I suppose it is in the Sub ConfirmRegistration() (don't have something with line count here).
Now. I think it would be a better idea to first check if the recordset is empty.
Suppose it is empty, then you now check
If RS.fields("confirmed") = True
but there will be no record to check on.
This probably causes the error. In any case, you should change it + also check if the record was updated.
So your code would look sometjing like

If rs.EOF = true then
response.write("The email was not found, you still need to register or whatever")
else
If RS.fields("confirmed") = True Then
Response.Redirect("/login.asp") ' This sends them to the login page letting it know they came from here
Else
Dim UpdateConfirmedStatus, numupdated
numupdated = 0
UpdateConfirmedStatus = "UPDATE members SET confirmed = True WHERE email = " & emc & " and userid = " & uid
Conn.Execute(UpdateConfirmedStatus), numupdated

if numupdated = 1 then
response.write("<p align=center><b>Thankyou " & RS.fields("First_Name") & " for registering click <a href=/login.asp>here</a> to login</b></p>")
Else
response.write("Databaseproblem")
end if
end if
end if

Crash1hd
05-14-2003, 08:55 AM
yep that pretty much did it the code I think I was having trouble with or at least was the most different was this

UpdateConfirmedStatus = "UPDATE members SET confirmed = True WHERE email = " & emc & " and userid = " & uid

but just adding

If rs.EOF = true then
emailfound = False ' The email was not found, they still need to register
else

at the beginning of most of it worked!

P.s. Sorry I will remember to bold in the future lol

:)

raf
05-14-2003, 09:34 AM
No problem. Glad you got it working.