Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Checking to see if a cookie exists

Checking to see if a cookie exists

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gavin_Mannion
    wrote on last edited by
    #1

    Hi All, Quick question here, I need to check to see if a cookie exists when someone enters 1 of my pages. This is an intranet so I know that everyone accepts cookies but is there way to see if a certain cookie exists? :~ :~ :~ Thanks, Gavin

    J 1 Reply Last reply
    0
    • G Gavin_Mannion

      Hi All, Quick question here, I need to check to see if a cookie exists when someone enters 1 of my pages. This is an intranet so I know that everyone accepts cookies but is there way to see if a certain cookie exists? :~ :~ :~ Thanks, Gavin

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      Check the cookie for null :) James Simplicity Rules!

      G 1 Reply Last reply
      0
      • J James T Johnson

        Check the cookie for null :) James Simplicity Rules!

        G Offline
        G Offline
        Gavin_Mannion
        wrote on last edited by
        #3

        Tried that, for some reason the page falls over if I try the following if (HttpContext.Current.Request.Cookies["NewOutbreak"]["Test"] == "") { //Do Something amazing } else { Go home } I get an error saying something like cannot check for null as it doesn't exist.??

        J 1 Reply Last reply
        0
        • G Gavin_Mannion

          Tried that, for some reason the page falls over if I try the following if (HttpContext.Current.Request.Cookies["NewOutbreak"]["Test"] == "") { //Do Something amazing } else { Go home } I get an error saying something like cannot check for null as it doesn't exist.??

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          I wonder if you are getting the error from the "NewOutbreak" cookie not existing and then trying to access it. Give this a shot... Request r = HttpContext.Current.Request; if(r.Cookies["NewOutbreak"] == null) { // NewOutbreak cookie is not set } else if( r.Cookies["NewOutbreak"]["Test"] == null) { // NewOutbreak.Test cookie is not set } else { // NewOutbreak.Test cookie IS set } I'm still working out your issue at the top :) James Simplicity Rules!

          G 1 Reply Last reply
          0
          • J James T Johnson

            I wonder if you are getting the error from the "NewOutbreak" cookie not existing and then trying to access it. Give this a shot... Request r = HttpContext.Current.Request; if(r.Cookies["NewOutbreak"] == null) { // NewOutbreak cookie is not set } else if( r.Cookies["NewOutbreak"]["Test"] == null) { // NewOutbreak.Test cookie is not set } else { // NewOutbreak.Test cookie IS set } I'm still working out your issue at the top :) James Simplicity Rules!

            G Offline
            G Offline
            Gavin_Mannion
            wrote on last edited by
            #5

            :cool: werkin :cool: I see the logic, you have to check to see if the Cookie exists before checking to see if the cookies key exists or has any data... Seems to be 100% Thanks :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups