473,245 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

ASP Login Script not working

Applies to: Microsoft FrontPage 2000, Microsoft Access 2000, IIS 5.0
Operating System: Microsoft Windows 2000 Professional

I am trying to protect a portion of a web site by allowing users to register a username and password & then login with those details, but so far I am having only marginal success. I am far from an expert on ASP programming, indeed the code I am using comes from "Sams Teach Yourself E-Commerce Programming with ASP" but it is ideally suited for my purpose.

In short, there are 3 .asp pages (register.asp, login.asp & checkpassword.asp - the code for each is below), a global.asa file was automatically created and by following the instructions in the book, I also created a small Access database called UserDB.mdb, which stores the username & password of each user when they register & also verify's those details when the user attempts to login again.

The DNS connection has been setup within FrontPage and I have verified that this connection works by clicking "Tools", "Web Settings" & the "Database" tab, highlighting the DNS connection & clicking Verify.

The problems seem to occur when I try to register a new username & password, for some strange reason the details I enter are not being saved in the database table, and to compound the problem further, if I register just a username, or a password but not both, the page simply refreshes itself with empty boxes instead of giving an error message to indicate that a "username" or "password" must be entered, which if I have read the code correctly on the "checkpassword.asp" page, should happen.

To further confuse the situation, if I manually enter a username & password into the database table and then attempt to click a hyperlink taking me to a "test.asp" page, with the INCLUDE FILE: <!-- #INCLUDE FILE="checkpassword.asp" -->, I am automatically taken to the login.asp, where if I enter the username & password that I manually put into the database table, it takes me to the selected "Protected" web page. In my mind that clearly shows the DNS connection is working but yet it won't store new registered details into the database table, which is extremely confusing.

If anyone can see what I may be doing wrong, or point me in the right direction, your help & advice will be greatly appreciated. As I pointed out earlier I am far from an expert, so any help you can give would be ideally suited towards a newbie mentality.

Below is the code for the three .asp pages:

Many thanks in advance
Wayne Smith

register.asp

<%
nextPage = Request( "nextPage" )

newUsername = Request( "newUsername" )

newPassword = Request( "newPassword" )

%>

<HTML>

<HEAD><TITLE>Register"</TITLE></HEAD>

<BODY>

Register at this Web site by selecting a username and password:

<FORM METHOD="post" ACTION="<%=nextPage%>">

<INPUT NAME="newUser" TYPE="hidden" VALUE="1">

<P><B>USERNAME:</B>

<INPUT NAME="newUsername" SIZE=20 MAXLENGTH="20"

VALUE="<%=Server.HTMLEncode( newUsername )%>">

<P><B>PASSWORD:</B>

<INPUT NAME="newPassword" SIZE=20 MAXLENGTH="20"

VALUE="<%=Server.HTMLEncode( newPassword )%>">

<P><INPUT TYPE="submit" VALUE="Register!">

</FORM>

</BODY>

</HTML>

----------------------------------------------------------------------------------

login.asp

<HTML>

<HEAD><TITLE>Login</TITLE></HEAD>

<BODY>

<%=loginMessage%>

<FORM METHOD="post" ACTION="<%=nextPage%>">

<P><B>USERNAME:</B>

<INPUT NAME="username" SIZE=20 MAXLENGTH="20"

VALUE="<%=Server.HTMLEncode( username )%>">

<P><B>PASSWORD:</B>

<INPUT NAME="password" SIZE=20 MAXLENGTH="20"

VALUE="<%=Server.HTMLEncode( password )%>">

<p><INPUT NAME="addCookie" TYPE="Checkbox" VALUE="1"> Remember me with a cookie

<P><INPUT TYPE="submit" VALUE="Login">

</FORM>

<p>

<a href="register.asp?nextpage=<%Server.URLEncode( nextpage )%>">

Click here to register</a>

</BODY>

</HTML>

-------------------------------------------------------------

checkpassword.asp

<%

CONST useSession = TRUE

' Retrieve Form Variables

username = TRIM( Request( "username" ) )

password = TRIM( Request( "password" ) )

newUser = TRIM( Request( "newUser" ) )

newUsername = TRIM( Request( "newUsername" ) )

newPassword = TRIM( Request( "newPassword" ) )

addCookie = TRIM( Request( "addCookie" ) )

' Retrieve Current Page

nextPage = Request.ServerVariables( "SCRIPT_NAME" )

' Ready Database Connection

Set Con = Server.CreateObject( "ADODB.Connection" )

Con.Open "userDNS"

' Add New User

IF newUser <> "" THEN

IF newUsername = "" THEN

showError "You must enter a username"

END IF

IF newPassword = "" THEN

showError "You must enter a password"

END IF

IF usernameTaken( newUsername ) THEN

showError "The username you entered has already " &_

"been chosen by a previous user. Please select " &_

"a new username"

END IF

sqlString = "INSERT INTO userlist ( user_username, user_password ) " &_

"VALUES ('" & newUsername & "','" & newPassword & "')"

Con.Execute sqlString

username = newUsername

password = newPassword

IF useSession THEN Session( "loggedIn" ) = "Yes"

END IF

' Authenticate User

IF Session( "loggedIn" ) = "" THEN

IF username = "" OR password = "" THEN

loginMessage = "You must login before you can view this page."

showLogin

END IF

result = validateLogin( username, password )

IF result = 1 THEN

loginMessage = "You entered an unregistered username."

showLogin

END IF

IF result = 2 THEN

loginMessage = "You did not enter a valid password."

showLogin

END IF

IF useSession THEN Session( "loggedIn" ) = "Yes"

END IF

' Add a Cookie

IF addCookie <> "" THEN

Response.Cookies( "username" ) = username

Response.Cookies( "username" ).Expires = "12/25/2037"

Response.Cookies( "password" ) = password

Response.Cookies( "password" ).Expires = "12/25/2037"

END IF

' Create Security Query String Variable

sq = "username=" & Server.HTMLEncode( username ) & "&"

sq = sq & "password=" & Server.HTMLEncode( password )

' Create Security Form Variable

sf = "<input name=""username"" type=""hidden"" "

sf = sf & "value=""" & Server.HTMLEncode( username ) & """>"

sf = sf & "<input name=""password"" type=""hidden"" "

sf = sf & "value=""" & Server.HTMLEncode( password ) & """>"

' Check Username and Password

FUNCTION validateLogin( theUsername, thePassword )

sqlString = "SELECT user_password FROM userlist " &_

"WHERE user_username='" & fixQuotes( username ) & "'"

Set RS = Con.Execute( sqlString )

IF RS.EOF THEN

validateLogin = 1

ELSE

IF RS( "user_password" ) <> thePassword THEN

validateLogin = 2

ELSE

validateLogin = 0

END IF

END IF

END FUNCTION

' Check Whether Username Already Taken

FUNCTION usernameTaken( theUsername )

sqlString = "SELECT user_id FROM userlist " &_

"WHERE user_username='" & fixQuotes( theUsername ) & "'"

Set RS = Con.Execute( sqlString )

IF RS.EOF THEN

usernameTaken = FALSE

ELSE

usernameTaken = TRUE

END IF

RS.Close

Set RS = Nothing

END FUNCTION

' Show Error Page

SUB showError( theError )

%>

<HTML>

<HEAD><TITLE>Problem</TITLE></HEAD>

<BODY>

<b>There was a problem with your registration information</b>

<br><%=theError %>

<FORM METHOD="POST" ACTION="register.asp">

<INPUT NAME="nextpage" TYPE="hidden"

VALUE="<%=nextpage%>">

<INPUT NAME="newUsername" TYPE="hidden"

VALUE="<%=Server.HTMLEncode( newUsername )%>">

<INPUT NAME="newPassword" TYPE="hidden"

VALUE="<%=Server.HTMLEncode( newPassword )%>">

<INPUT TYPE="SUBMIT" VALUE="Continue">

</FORM>

</BODY>

</HTML>

<%

Response.End

END SUB

' Show the Login Page

SUB showLogin

%>

<!-- #INCLUDE FILE="login.asp" -->

<%

Response.End

END SUB

FUNCTION fixQuotes( theString )

fixQuotes = REPLACE( theString, "'", "''" )

END FUNCTION

%>

Jul 19 '05 #1
1 5423
Yes there seem to be a lot of problems with that code.

In login.asp since nextpage is never set the URL is something like
register.asp?nextpage=:
<<
<a href="register.asp?nextpage=<%server.urlencode( nextpage )%>">

Click here to register</a>


There is no code in any of the 3 pages that actually writes to the
database.

In checkpassword.asp included file there is not End Function at the end
or the %> end of ASP end tag.

And by the way here is a simple overview of how I deal with the login of
an already registered user.

Perhaps have a login page that asks the user for his username and
password. And whatever page that posts to (which could be the same page
for a self posting form) tests these fields against what is in the
database, sets the username and userlevel session variables accordingly,
and then redirects to the proper page - i.e. back to the login page if
the password is wrong (perhaps with a JavaScript popup saying wrong
username/password combination) or to the main menu page if the password
is correct:

Session("UserName") = objRS("UserName")
Session("UserLevel") = objRS("UserLevel")
Response.Redirect "mainmenu.asp"

Then you can use If Then's or Select Case on each page to control
whether a user is allowed to actually be there and whether particular
links of where a user can go actually show up.

If (Session("UserLevel") <> "Admin") And (Session("UserLevel") <>
"Regular") Then
Response.Redirect "login.asp"
End If

Best regards,
J. Paul Schmidt, Freelance ASP Web Designer
http://www.Bullschmidt.com
ASP Designer Tips, ASP Web Database Demo, Free ASP Bar Chart Tool...
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: koolyio | last post by:
Hey, could you please tell me what is wrong with my login script. I just started learning php. CODE: login.php <? session_start(); header("Cache-Control: private"); ?>
4
by: Joe | last post by:
Hi, I have MS Access database with a small table that has four columns ID (Primary Key), page, userid and password. The page column holds the name of the page which has the login form. The...
20
by: Navillus | last post by:
Hey gang, I'm helping this guy with his company's website. They have a page where you login, and basically it looks like when you enter your password and user info and submit the form, it runs a...
5
by: Kivak Wolf | last post by:
Hi, I am currently trying to make a website that requires an account to use so it can disply the data according to the account. I have ASP.NET 2.0 installed and working fine. However, it is not...
1
by: Jakob Lithner | last post by:
When I started a new ASP project I was eager to use the login facilities offered in Framework 2.0/VS 2005. I wanted: - A custom principal that could hold my integer UserID from the database -...
1
by: Brad Isaacs | last post by:
Good evening friends, I am working with Visual Studio 2005, ASP.NET 2.0 I am working with the Login controls provided my .NET 2.0, trying to make the Login1 control UserName textbox obtain...
5
by: muppetjones | last post by:
I'm really new to the whole networking side of things, so I don't know the backend very well. I wrote a series of PHP/AJAX scripts to allow a user to create a login account, but apparently my script...
3
by: harsh9892991141 | last post by:
every time enter my login id and password in my index page it shows me the error 404 Object not found! The requested URL was not found on this server. The link on the referring page seems to be...
4
by: adam.waterfield | last post by:
I am just wondering if anyone here can help with a problem that we have here. We have a PHP based website/application that has a user login that is connected to our AD setup. This works fine....
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.