ASP and if statement using cookies

Discussion in 'C#' started by samfourie, Aug 21, 2009.

  1. #1
    Please help !

    i have a page where a list of statements are made. each statement can have a positive or negative answer in format of a "plus" or a "minus" response.

    in the database the fields are text and are incremented by a negative or positive number.

    in oder to prevent the same person clicking twice on the same item i need to read and or inject a cookie to see if he has previously responded to each item

    here is my code and this is where my technical ASP skills came tumbling down

    <%
    url = Request.QueryString("id")
    ##################################################
    requesting the id of the statement
    ##################################################


    fname = Request.Cookies("TwitPlus")
    ##################################################
    to read a cookie if there is one ? and to see if the cookie exist ?
    ##################################################


    if fname = url then
    ##################################################
    HELP HELP HELP
    How do i identfy and compare this ID with a cookie if it existys and if it does how do i reject his path and let him go back to the default page
    ##################################################



    set conn = Server.CreateObject("ADODB.Connection")
    conn.open Conn_String
    Set RS = conn.Execute("select * from Table_Contacts where Twit_ID =" & url)
    hit=cInt(rs("Twit_Main_Vote_Plus"))+1
    Set RS2 = conn.Execute("UPDATE Table_Contacts SET Twit_Main_Vote_Plus = " & hit & " Where Twit_ID =" & url)
    RS.Close
    conn.Close
    set conn = nothing
    ##################################################
    my code incrementing the number in the field
    this work okay without my jumbled cookie code
    ##################################################


    Response.Cookies("TwitPlus") = "TwitPlus" & Twit_ID
    Response.Cookies("TwitPlus").Expires=#May 10,2020#
    ##################################################
    here i write a cookie if he has not made his vote on this stament
    ##################################################

    Response.Redirect "default.asp"
    else
    ##################################################
    here i redirect him back to0 the page
    ##################################################

    Response.Redirect "default.asp"
    end if
    ##################################################
    here i redirect him back to0 the page
    ##################################################

    %>
     
    samfourie, Aug 21, 2009 IP
  2. saraswat

    saraswat Guest

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hi, i want to know more about asp..so any best site for asp????
    --
    aad!:confused:
     
    saraswat, Aug 22, 2009 IP
  3. saraswat

    saraswat Guest

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi, i want to know more about asp..so any best site for asp????
    --
    aad!:confused::confused:
     
    saraswat, Aug 22, 2009 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I learn a lot from w3schools
     
    camjohnson95, Aug 23, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    oh and here is a solution:
    
    <%@Language=VbScript %>
    <%
    'Declare your variables, it is not necessary with ASP but is good habit to get into.
    Dim url,k
    
    'The following line stops the browser from caching.
    Response.Expires = -1
    
    'Set the cookie expiry date.
    Response.Cookies("TwitPlus").Expires = #May 10,2020#
    
    'Retrieve url from querystring
    url = Request.QueryString("id")
    
    'executes the function beenvisited to return true or false 
    If beenVisited Then
        Response.Write "Has Been visited"
    Else
    	 'if the page has not been visited execute subprocedure writeCookie
        writeCookie
        Response.Write "Has not been visited"
    End If
    
    Function beenVisited()
        beenVisited = False
    	 'iterates through all the keys within the TwitPlus cookie, and
        'checks for values that equal that of the id.
        'Cookies can be a regular value, e.g text or a number but they can
        'also be a collection, or an array. This is what we are using in this case.
        For Each k In Request.Cookies("TwitPlus")
            If Request.Cookies("TwitPlus")(k) = url Then beenVisited=True
        Next
    End Function
    
    Sub writeCookie()
        'adds a new value to the TwitPlus cookie, we use the url for both the key and
        'the value, but as long as the key is unique it doesn't matter.
        Response.Cookies("TwitPlus")(url) = url
    End Sub
     %>
    
    Code (markup):
     
    camjohnson95, Aug 23, 2009 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    A few things that you need to add to the code is:
    1. Check that the id actually has a valid value.
    2. Check that the id, actually exists within the database.
    3. Stop referring to your users as twits, even if it is only within the code.
     
    camjohnson95, Aug 23, 2009 IP