Solved

Help with cookies

Posted on 2004-03-22
13
238 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
ID: 10650890
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
ID: 10650992
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
ID: 10651006
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
ID: 10651048
Please Post the Whole error message
0
 
LVL 1

Author Comment

by:sre23
ID: 10651123
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
ID: 10651268
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:adilkhan
ID: 10651288
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
ID: 10651444
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
ID: 10651538
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
ID: 10651570
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
ID: 10651662
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
ID: 10651807
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
ID: 10651829
No problem:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
2 containers on one webpage 4 249
Adobe Web Premium CS3 5 374
Question about Dreamweaver CC 1 302
drop down button disappears 12 99
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…
This article is very specific and is only intended to help if you are installing Dreamweaver 8 in a Windows 7 environment with Office 2007 installed.   I'm not sure why Microsoft tends to release OS' that should not be released but they do.  Windows…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

19 Experts available now in Live!

Get 1:1 Help Now