Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 10th, 2003, 09:43 AM
Authorized User
 
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating a new cookie

Hi,

I have an asp page where i created cookies-for admin and for user. The aspx page reads these cookies. Everything seems to work fine but now i have to create a new cookie if cookie has expired or if cookie is null. Also, i am having problems with the expire because if the username belongs to the admin for example and then i want to change the username that belongs to a normal user the cookie recognises the first (admin) cookie. How do i solve this and how do i create a new cookie?
Thanks
if ACCESS="user" then
'--create user cookie
    Response.Cookies("UserCookie")("Username") = Request.Form("UserName")
    Response.Cookies ("UserCookie")("Password")=Request.Form ("Password")
    Response.Cookies("UserCookie").Path = "/"
    Response.Cookies ("UserCookie").expires = DateAdd("d", Date, 14)
    Response.Write(Request.Cookies("UserCookie")("User name"))
    Response.Write (Request.Cookies("UserCookie")("Password"))

    '---end user cookie
   else
    'create admin cookie

    Response.Cookies("adminCOOKIE")("ADMINusername") = Request.Form("UserName")
    Response.Cookies ("adminCOOKIE")("ADMINPassword")=Request.Form ("Password")
    Response.Cookies("adminCOOKIE").Path = "/"
    Response.Cookies ("adminCOOKIE").expires =dateadd("m",20,now())
    Response.Write(Request.Cookies("adminCOOKIE")("ADM INusername"))
    Response.Write (Request.Cookies("adminCOOKIE")("ADMINPassword"))

    '---end admin cookie
    end if
   end if


 
Old November 10th, 2003, 11:42 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Is that the code you are currently using? How do your questions relate to that? Aren't you looking in too different places for the admin and user cookies?

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 10th, 2003, 04:03 PM
Authorized User
 
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

This is the code for my ASP page. I created 2 different cookies-one for user and one for admin. My aspx page reads those cookies.
However, when i set the cookies to expire then the code doesn't work. If i logged in first as admin and then close my browser and want to login as a user then i can't as it recognises only the first cookie created. I need to change that. also, if 2 users are using the same computer and the first user logs out then i don't want the second user to be able to use the first user's cookie.

the aspx code:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim cnPoster As New ADODB.Connection
        Dim MM_valUsername As String

 cnPoster.Open("Provider=Microsoft.Jet.OLEDB.4.0;Da ta Source=" & Server.MapPath("/duclassified.mdb"))


        Dim Username As String
        Dim Password As String
        Dim adminU As String
        Dim adminP As String

    Dim adPoster As String

        Try

 adminU = (Server.UrlDecode(Request.Cookies("adminCOOKIE").V alues("ADMINusername")))
 adminP = (Server.UrlDecode(Request.Cookies("adminCOOKIE").V alues("ADMINPassword")))


        Catch ex As Exception
            Dim ad_idP = Request.QueryString("ad_id")
            Dim rsposter As New ADODB.Recordset

rsposter.Open("Select ad_poster from ads where ad_ID =" & ad_idP, cnPoster)

adPoster = (rsposter("ad_poster").Value)

            Try
Username = (Server.UrlDecode(Request.Cookies("UserCookie").Va lues("Username")))
Password = (Server.UrlDecode(Request.Cookies("UserCookie").Va lues("Password")))


                If adPoster <> Username Then
                    Response.Redirect("myads.asp")
                End If


            Catch exc As Exception
                Response.Redirect("myAds.asp")
            End Try

        End Try


        If Not Page.IsPostBack Then
            'code goes here
        End If


 
Old November 10th, 2003, 04:29 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well... you should probably check that the cookie exists before you try to use it. It sounds like it's expiring ok. Cause then you have problems cause it's not there. So check to see if it's there, then do whatever's necessary.

Use this format:

If Not Request.Cookies("adminCOOKIE") Is Nothing Then
    'Do what you need when cookie is there
Else
    'No cookie, now what?
End If

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 11th, 2003, 09:27 AM
Authorized User
 
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thanks for the response. I tried your way but probably i didn't understand it so it didn't work. However, I tried another way and it seems to work fine. The cookie gets deleted. Before what happened was that it only recognised the first username and password entered and if i entered a different username and passwork then it didn't recognise it.
 I am not sure that this is what my team leader meant but this is what i did and it seems to work fine:
DIM ACCESS
DIM U_ID
DIM U_PWORD
set rsACCESS = Server.CreateObject("ADODB.Recordset")
rsACCESS.ActiveConnection = MM_connDUclassified_STRING
rsACCESS.Source = "SELECT U_ID,U_PASSWORD, ACCESS FROM USERS WHERE U_ID = '" + CStr(Request.Form("Username")) + "'"
rsACCESS.CursorType = 0
rsACCESS.CursorLocation = 2
rsACCESS.LockType = 3
rsACCESS.Open()
rsACCESS_numRows = 0
access=(rsACCESS.fields.item ("ACCESS").value)
 u_id=(rsACCESS.fields.item ("u_id").value)
 U_PWORD=(rsACCESS.fields.item ("u_password").value)
     '---create user cookie

    if ACCESS="user" then
   Response.Cookies("adminCOOKIE").Path = "/"
       Response.Cookies("adminCOOKIE")=""
     Response.Cookies("adminCOOKIE").Expires=date ()-100
    Response.Cookies("UserCookie")("Username") =Request.Form("UserName")
    Response.Cookies ("UserCookie")("Password")=Request.Form ("Password")
    Response.Cookies("UserCookie").Path = "/"
    Response.Cookies ("UserCookie").expires = DateAdd("d", Date, 14)
    Response.Write(Request.Cookies("UserCookie")("User name"))
    Response.Write (Request.Cookies("UserCookie")("Password"))

    '---end user cookie
        else
    'create admin cookie

    Response.Cookies("UserCOOKIE")=""
      Response.Cookies("UserCookie").Path = "/"
     Response.Cookies("UserCookie").Expires=date ()-100
    Response.Cookies("adminCOOKIE")("ADMINusername") = Request.Form("UserName")
    Response.Cookies ("adminCOOKIE")("ADMINPassword")=Request.Form ("Password")
    Response.Cookies("adminCOOKIE").Path = "/"
    Response.Cookies ("adminCOOKIE").expires =dateadd("m",20,now())
    Response.Write(Request.Cookies("adminCOOKIE")("ADM INusername"))
    Response.Write (Request.Cookies("adminCOOKIE")("ADMINPassword"))

    '---end admin cookie

' end if
   end if
   rsACCESS.close
    %>







Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookie prv299 ASP.NET 2.0 Professional 3 May 29th, 2008 11:26 AM
cookie turn off swagatika ASP.NET 1.0 and 1.1 Basics 2 March 15th, 2006 09:25 AM
COOKIE problem Buravik PHP How-To 2 May 9th, 2004 08:47 AM
login cookie help daddycool2k Classic ASP XML 26 December 4th, 2003 10:45 AM
login cookie help daddycool2k ASP.NET 1.0 and 1.1 Basics 0 November 16th, 2003 03:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.