Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp Remember me Login!

Status
Not open for further replies.

techskool

Technical User
Jun 20, 2002
151
GB
I am thourougly baffled with a login, that im trying to ensure is remembered when a checkbox is selected at login.

I have:

valRemember = Request.Form("remember")

If Session(&quot;Username&quot;) <> &quot;&quot; And Session(&quot;Password&quot;) <> &quot;&quot; Then
valUsername = Session(&quot;Username&quot;)
valPassword = Session(&quot;Password&quot;)
Else
valUsername = Request.Form(&quot;Username&quot;)
valPassword = Request.Form(&quot;Password&quot;)
End If

'connection object
set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open Application(&quot;strConn&quot;)
'recordset object
set rs = server.createobject(&quot;adodb.recordset&quot;)
'set sql
sql=&quot;SELECT Username, Password FROM owner WHERE Username='&quot; & valUsername &&quot;' AND Password='&quot; & valPassword & &quot;'&quot;
'open everything
rs.open sql, conn, 1


'create the session here for the logged in user - session value is the username
'and can be referenced throughout the site with 'session(&quot;Username&quot;)'
If Not rs.EOF Then
Session(&quot;Username&quot;) = rs(&quot;Username&quot;)
Session(&quot;Password&quot;) = rs(&quot;Password&quot;)
If remember <> &quot;&quot; Then
Session.Timeout = 300
End If
Response.Redirect(&quot;records/admin_scripts/index.asp&quot;)
End If
'close recordset
rs.Close
%>


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

Im basically tring to ensure that when someone logs in, and opts to be remembered when they close their Internet explorer and go back to the login page, 2 sessions for username and password are remembered, tested in the SQL and all is good.

Anyone see what is wrong with the code.

Thanks

Dave
 
You cannot use Session in this case. After IE is closed, all session variables will be gone. When user opens new IE, new session varialbes will be assigned to that IE (exactly to that SessionID).

You have to use cookies to save username and password on client's PC, so that, you can retrieve them the next time he goes to your page.

 
Bugger, i new i was going round in circles!!!

Thankyou very much phuctran!

:)
 
Im trying it with Cookies now, but its not even letting me log in now

Am i using the Cookie object correctly here?
---------------------------------
valRemember = Request.Form(&quot;remember&quot;)
UserCook = Request.Cookies(&quot;LoggedIn&quot;)(&quot;Username&quot;)
PassCook = Request.Cookies(&quot;LoggedIn&quot;)(&quot;Password&quot;)

If UserCook <> &quot;&quot; And PassCook <> &quot;&quot; Then
valUsername = UserCook
valPassword = PassCook
Else
valUsername = Request.Form(&quot;LoggedIn&quot;)
valPassword = Request.Form(&quot;LoggedIn&quot;)
End If

set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open Application(&quot;strConn&quot;)
set rs = server.createobject(&quot;adodb.recordset&quot;)
sql=&quot;SELECT Username, Password FROM owner WHERE Username='&quot; & valUsername &&quot;' AND Password='&quot; & valPassword & &quot;'&quot;
rs.open sql, conn, 2

'create the session here for the logged in user
If Not rs.EOF Then
Session(&quot;Username&quot;) = valUsername
If remember <> &quot;&quot; Then
' create cookies to remember
valUsername = Response.Cookies (&quot;LoggedIn&quot;)(&quot;Username&quot;)
valPassword = Response.Cookies (&quot;LoggedIn&quot;)(&quot;Password&quot;)
Response.Redirect(&quot;records/admin_scripts/index.asp&quot;)
End If
Response.Redirect(&quot;records/admin_scripts/index.asp&quot;)
End If
rs.Close


---------------------------------
Thanx again

Dave :)
 
Hi Gary

Thanks for that, hehe must be tired!

My final go works for the login and session, but isnt writing a cookie.

Im probably just going to use a sessio, but id have liked to have figured out cookies.

final code is:

-----
valRemember = Request.Form(&quot;remember&quot;)
Request.Cookies(&quot;LoggedIn&quot;)
UserCook = Request.Cookies(&quot;LoggedIn&quot;)(&quot;Username&quot;)
PassCook = Request.Cookies(&quot;LoggedIn&quot;)(&quot;Password&quot;)

'get form info if cokkies dont exist
If UserCook = &quot;&quot; And PassCook = &quot;&quot; Then
valUsername = Request.Form(&quot;Username&quot;)
valPassword = Request.Form(&quot;Password&quot;)
Else
'retrieve cokkie values
valUsername = UserCook
valPassword = PassCook
End If

'connect
set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open Application(&quot;strConn&quot;)
set rs = server.createobject(&quot;adodb.recordset&quot;)
sql=&quot;SELECT Username, Password FROM owner WHERE Username='&quot; & valUsername &&quot;' AND Password='&quot; & valPassword & &quot;'&quot;
rs.open sql, conn, 2

'create session
If Not rs.EOF Then
Session(&quot;Username&quot;) = valUsername
If valRemember <> &quot;&quot; Then
' create new cookie
Response.Cookies(&quot;LoggedIn&quot;)(&quot;Username&quot;) = valUsername
Response.Cookies(&quot;LoggedIn&quot;)(&quot;Password&quot;) = valPassword
End If
Response.Redirect(&quot;records/admin_scripts/index.asp&quot;)
End If
rs.Close
-----



Thanks once more

Dave

:)
 
Hmmm think you're messing up in your coding a bit.
You should be setting the values of the username and password boxes on the login Form from the cookie values and not checking them after they submit the form to login.
Or as in the case of this site just automatically logging them in.
Following me?

 
Can you give me an example

The login page calls itself from when you submit Username and password, and redirects before writing the form out again if there are successful details

so the first time you use the page it can be for a login.

then when you close the browser and open it again at the login page, i wanted it to redirect

Dave

:)
 
On your home page have this code
<%
UserCook = Request.Cookies(&quot;LoggedIn&quot;)(&quot;Username&quot;)
PassCook = Request.Cookies(&quot;LoggedIn&quot;)(&quot;Password&quot;)
if UserCook<>&quot;&quot; and PassCook<>&quot;&quot; then
set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open Application(&quot;strConn&quot;)
set rs = server.createobject(&quot;adodb.recordset&quot;)
sql=&quot;SELECT Username, Password FROM owner WHERE Username='&quot; & valUsername &&quot;' AND Password='&quot; & valPassword & &quot;'&quot;
rs.open sql, conn
If Not rs.EOF Then Session(&quot;Username&quot;) = UserCook
rs.Close
end if
%>


Then for your check login code....
<%
valRemember = Request.Form(&quot;remember&quot;)
valUsername = Request.Form(&quot;Username&quot;)
valPassword = Request.Form(&quot;Password&quot;)

valUsername = replace(valUsername,&quot;'&quot;,&quot;&quot;)
valPassword = replace(valPassword,&quot;'&quot;,&quot;&quot;)

set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open Application(&quot;strConn&quot;)
set rs = server.createobject(&quot;adodb.recordset&quot;)
sql=&quot;SELECT Username, Password FROM owner WHERE Username='&quot; & valUsername &&quot;' AND Password='&quot; & valPassword & &quot;'&quot;
rs.open sql, conn

If Not rs.EOF Then
Session(&quot;Username&quot;) = valUsername
If valRemember <> &quot;&quot; Then
Response.Cookies(&quot;LoggedIn&quot;)(&quot;Username&quot;) = valUsername
Response.Cookies(&quot;LoggedIn&quot;)(&quot;Password&quot;) = valPassword
End If
Response.Redirect(&quot;records/admin_scripts/index.asp&quot;)
End If
rs.Close
%>

Of course you will need some code if they no longer want you to remember them!

 
Right, i actually know less know than when i started, but thanks for all the help so far. :)

I have a link to a login page. This page submits to itself then redirects to the staff area on sucess, of just refreshes if the login is incorrect.

I have (thanks to your help)
----------------
LOGIN PAGE:
<%
'initially try and login if a cookie exists
Response.Buffer = True
UserCook = Request.Cookies(&quot;LoggedIn&quot;)(&quot;Username&quot;)
PassCook = Request.Cookies(&quot;LoggedIn&quot;)(&quot;Password&quot;)
if UserCook<>&quot;&quot; and PassCook<>&quot;&quot; then
set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open Application(&quot;strConn&quot;)
set rs = server.createobject(&quot;adodb.recordset&quot;)
sql=&quot;SELECT Username, Password FROM owner WHERE Username='&quot; & UserCook &&quot;' AND Password='&quot; & PassCook & &quot;'&quot;
rs.open sql, conn
If Not rs.EOF Then Session(&quot;Username&quot;) = UserCook
rs.Close
Response.Redirect(&quot;records/admin_scripts/index.asp&quot;)
end if

'if a cookie doesnt exist try and login with posted from data
valRemember = Request.Form(&quot;remember&quot;)
valUsername = Request.Form(&quot;Username&quot;)
valPassword = Request.Form(&quot;Password&quot;)
set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open Application(&quot;strConn&quot;)
set rs = server.createobject(&quot;adodb.recordset&quot;)
sql=&quot;SELECT Username, Password FROM owner WHERE Username='&quot; & valUsername &&quot;' AND Password='&quot; & valPassword & &quot;'&quot;
rs.open sql, conn

If Not rs.EOF Then
Session(&quot;Username&quot;) = valUsername
If valRemember <> &quot;&quot; Then
Response.Cookies(&quot;LoggedIn&quot;)(&quot;Username&quot;) = valUsername
Response.Cookies(&quot;LoggedIn&quot;)(&quot;Password&quot;) = valPassword
End If
Response.Redirect(&quot;records/admin_scripts/index.asp&quot;)
End If
rs.Close
%>
----------------------------------------



STAFF PAGES - all which have as an include a file to ensure you are logged in:
----------------------------------------
<%
If Session(&quot;Username&quot;) = &quot;&quot; Then
Response.Redirect(&quot;../../login.asp&quot;)
end if
%>
----------------------------------------


SIGN OUT PAGE to clear everything:
----------------------------------------
<% ' terminate
response.buffer = true
Request.Cookies(&quot;LoggedIn&quot;)
Response.Cookies(&quot;LoggedIn&quot;)(&quot;Username&quot;) = &quot;&quot;
Response.Cookies(&quot;LoggedIn&quot;)(&quot;Password&quot;) = &quot;&quot;
Session(&quot;Username&quot;) = &quot;&quot;
Response.Redirect(&quot;../../login.asp&quot;)
%>
----------------------------------------

At the moment it is working as it did earlier just with the session. When i log out and then come back to the login page it doesnt appear to be texting for the cookie.

I cannot see anything wrong.

Youve got

LOGIN PAGE

If cookies exist use them to validate entry then redirect to admin

Or submit to the login page and If cookies dont exist create them and a session then redirect to the admin page.

This login page serves 2 purposes (or should) If it is called with cookies present you will not see it, you will be taken straight to the admin area. If they arent present then you will use it to submit data - posting back to itself at which point the second bit of code will be executed (because the cookies dont exist yet) and we will be sent to admin.

Can you see what is happening, because after browser termintaion you should not be asked to fill your details in again, you should just be redirected to admin.

I am slowly losing the will to live

Any advice much appreciated

Dave

:)


 
You need one more information for the cookies (Expires)
Response.Cookies(&quot;LoggedIn&quot;).Expires = Dateadd(&quot;d&quot;, 3, Now)

That cookies will be expired 3 days later. If you don't have Expires, that cookies will be discarded when the browser is closed (like Session).
 
Its incredible isnt it, how such a small piece of code, and in some cases one piece of syntax, can be so vital to something working or not

Many kind Regards to phuctran and GaryC123

:)

Thankyou

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top