Solved

Help with cookies

Posted on 2004-03-22
13
237 Views
Last Modified: 2011-09-20
I'm trying to use cookies on my intranet site so that when users visit a certain password protected page, the page will look for a cookie to see if they have already logged in or not, and if they have it will forward them past the login page so they don't have to re-enter their username or password.  I believe the adding part is working (I can get a cookie onto the user's machine after they have successfully logged in once.  But my problem is reading this cookie from the users machine.  It seems that the cookie is only valid while the user's browser is open.  Once the browser is closed and the user tries to access that page again, I get a Page Cannot be Displayed error.  Here is the code:

To add the cookie after a user has logged in:
Response.Cookies("auth") = "yes"
Response.Cookies("auth").Expires = Date + 365

Then to read the cookie if they visit this login.asp page again:
MM_redirectLoginSuccess="Calendars/NewEvent.asp?ID="&Recordset1__MMColParam
MM_redirectLoginFailed="AccessDenied.htm"

If (Request.Cookies("auth") = "yes") Then
      Response.redirect(MM_redirectLoginSuccess)
End If

the MM_redirectLoginSuccess variable is the page that is displayed after you successfuly login and I want to automatically send the user there if they have this cookie (auth) with a value of yes.  

If you need to see more code just ask!

Thanks
0
Comment
Question by:sre23
  • 7
  • 6
13 Comments
 
LVL 23

Expert Comment

by:adilkhan
Comment Utility
You are missing the Key Value


try

Response.Cookies("MYCOOKIE")("auth") = "yes"

so add ("MYCOOKIE") to all of your Cookie Call's.

Cookie requires two parts.
1. Cookie name
2. Cookie Key

You must create the cookie before the HTTP headers are written to the client browser. Basically that means you need to create cookies before the <HTML> tag on your page.
0
 
LVL 1

Author Comment

by:sre23
Comment Utility
so when I add the cookie the code should be:

Response.Cookies("MYCOOKIE")("auth") = "yes"
Response.Cookies("MYCOOKIE")("auth").Expires = Date + 365

and then when I try to read the cookie it should be:

If (Request.Cookies("MYCOOKIE")("auth") = "yes") Then
    Response.redirect(MM_redirectLoginSuccess)
End If

Is this correct?
0
 
LVL 1

Author Comment

by:sre23
Comment Utility
that can't be right because when I try that I get a page cannot be displayed error when trying to add the cookie to the user's machine.  What am I missing?
0
 
LVL 23

Expert Comment

by:adilkhan
Comment Utility
Please Post the Whole error message
0
 
LVL 1

Author Comment

by:sre23
Comment Utility
Microsoft VBScript runtime error '800a01b6'

Object doesn't support this property or method: 'Response.Cookies(...)'

/MG_Info/Login.asp, line 56

Line 56 is Response.Cookies("MGC")("auth").Expires = Date + 365
0
 
LVL 1

Author Comment

by:sre23
Comment Utility
Here's the more generic error message:

The page cannot be displayed
There is a problem with the page you are trying to reach and it cannot be displayed.

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

Please try the following:

Open the mgc home page, and then look for links to the information you want.
Click the  Refresh button, or try again later.

Click  Search to look for information on the Internet.
You can also see a list of related sites.




HTTP 500 - Internal server error
Internet Explorer  
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 23

Expert Comment

by:adilkhan
Comment Utility
I am sorry, Syntax i mentioned above was for dictionary cookies, you dont really need it.

Code you posted is correct, I dont see any problem with it.

Your Cookies are disabled in browser?
and you generating cookies on the TOP of your page?
0
 
LVL 1

Author Comment

by:sre23
Comment Utility
yes the code is at the top of the page (before <html>) and I believe cookies are enabled because it works fine until I close the browser.  Once I close the browser and re-open it, it does not work.  I'm not sure if the part that fails is when the browser is trying to access the cookie, or if it fails when it is trying to redirect the user past the current page.

If you notice my redirect page uses a variable from the url and that code comes right before the request for the cookie.  Here is the code starting from the very top to the point that it tries to access the cookie and redirect the user past the login page (the current page).

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/conIntranet.asp" -->
<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "0"
If (Request.QueryString("ID") <> "") Then
  Recordset1__MMColParam = Request.QueryString("ID")
End If

MM_redirectLoginSuccess="Calendars/NewEvent.asp?ID="&Recordset1__MMColParam
MM_redirectLoginFailed="AccessDenied.htm"

If (Request.Cookies("auth") = "yes") Then      
      Response.redirect(MM_redirectLoginSuccess)
End If
0
 
LVL 23

Expert Comment

by:adilkhan
Comment Utility
Print the Value on the Top of your Page

<%=Request.Cookies("auth")%>
<% Response.end()%>

What does it show?
0
 
LVL 1

Author Comment

by:sre23
Comment Utility
ok, I found the problem... the redirect page has security enabled (Restrict Acces To Page behavior) so when I try to forward there automatically I am actually getting sent to the access denied page.  This becomes a whole different problem now.  Is there a way around that?  I've seen sites (such as this one) that have a "Remember Me" feature so that users don't need to log in each time.  I figured the easiest way to do that was with a cookie, but how do you do it?
0
 
LVL 23

Accepted Solution

by:
adilkhan earned 50 total points
Comment Utility
The way you doing is the way to store user name ans password on user Machine.
it is Done with Cookies.

That page has the restrict access, modify that Page to overwrite the Login Process if Cookie is set to True.
0
 
LVL 1

Author Comment

by:sre23
Comment Utility
thanks so much for your help... sorry to make you take the longest route to fix that one!
0
 
LVL 23

Expert Comment

by:adilkhan
Comment Utility
No problem:)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I still run into .cgi files every now and then. In some instances, I actually prefer the simplicity of a .cgi script to other options. Since I use DreamWeaver extensively, what I needed was a way to open .cgi scripts in Dreamweaver. And I wanted to …
Adobe Dreamweaver CS5 is a WYSIWYG web page editor that has advanced HTML, CSS, and Javascript rendering functionality and is probably the most well-known HTML editor available. Much of Dreamweaver's appeal centers around the Design View interfac…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now