Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check for cookies 1

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
I'm trying to do a check to see if the client browser has cookies enabled. But my code below always gives me the value for acceptsCookies = true, whether the machine has cookies enabled or not.

Can anybody help me out with this?


private void Page_Load(object sender, System.EventArgs e)
{
if (!(Page.IsPostBack))
{
//check browser accepts cookies
HttpCookie checkCookies = new HttpCookie("checkCookies");
checkCookies.Values["userName"] = "mike";
Response.Cookies.Add(checkCookies);

bool acceptsCookies = false;

if (Request.Cookies["checkCookies"].Values["userName"] == null)
{
acceptsCookies = false;
}
else
{
acceptsCookies = true;

//Delete test cookie
Response.Cookies["checkCookies"].Expires = DateTime.Now.AddDays(-1);
}

lblInfo.Text = acceptsCookies ? "Accepts cookies" : "Doesn't accept cookies";
}
}



Cheers,

lfc77
 
You can try using the Request.Cookies["checkCookies"].HasKeys property.

Code:
if (Request.Cookies["checkCookies"].HasKeys)
  CookieExists = true;
else
  CookieExists = false;

Keith
 
I've already looked at this URL and tried the code with no success. The only thing I can think of is that there are some settings in my web.config that are wrong?
 
Is it only possible to write cookies to a machine from a live website, or should they be written to your machine even when you are testing on your local machine? I wouldn't have thought you could write them from your local machine (mine doesn't write them), or maybe I am wrong?


 
You can write them to your own machine.
In the webconfig under sessionState is the line
cookieless="false"
there?
Marty
 
Cappmgr,

Are you using the standard web.config file generated by VS.NET? It doesn't write them to my machine at all, and I don't think there is anything wrong with my code (I'm using an example from MSDN.


 
I have no access to VS.NET.
The following code will display no cookie for two reasons
if the Cookie Expires is les than or equal to 0 or if cookies are turned off. Maybe it will help. Drop this on your machine and see if it works.
Code:
<%@ Page Language="C#" %>
<script runat="server">

    private void Page_Load(object sender, System.EventArgs e)
      {
          //CheckIfBrowserAcceptsCookies();
          if (!Page.IsPostBack)
          {
              lblCookieValue.Text = "Play with cookies";
          }
      }

      void btnSetCookie_Click(object sender, EventArgs e) {
          SetCookie(txtCookieName.Text, txtCookieValue.Text ,Convert.ToInt32(txtCookieExpire.Text));
      }

      public bool SetCookie(string cookiename, string cookievalue ,int iDaysToExpire)
      {
      try
      {
          HttpCookie objCookie = new HttpCookie(cookiename);
          Response.Cookies.Clear();
          Response.Cookies.Add(objCookie);
          objCookie.Values.Add(cookiename,cookievalue);
          DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
          Response.Cookies[cookiename].Expires =dtExpiry;
      }
          catch( Exception e){
          return false;
      }
          lblCookieValue.Text = "Cookie Changed";
          return true;
      }

      void btnGetCookie_Click(object sender, EventArgs e) {
          lblCookieValue.Text = GetCookie(txtCookieName.Text);
      }

      public string GetCookie(string cookiename){
      string cookyvalue="";
      try{
          cookyvalue= Request.Cookies[cookiename].Value;
      }
          catch(Exception e){
          cookyvalue="";
      }
          if (Request.Cookies[cookiename] != null)
          {
            return cookyvalue;
          }
          else
          {
            return "no cookie ";
          }
      }

      //Not used because it does not care if cookies are turned off  
      public void CheckIfBrowserAcceptsCookies()
      {
          HttpBrowserCapabilities objBrowsCap = new HttpBrowserCapabilities();
          if (Request.Browser.Cookies)
          {
              lblCookieValue.Text = "Your Browser Supports Cookies!";
          }
          else
          {
              lblCookieValue.Text = "Your Browser Does Not Support Cookies!";
          }
      }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:Label id="Label1" runat="server">Cookie Name </asp:Label>
        <asp:TextBox id="txtCookieName" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label id="Label2" runat="server">Cookie Value </asp:Label>
        <asp:TextBox id="txtCookieValue" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label id="Label3" runat="server">Cookie Expires</asp:Label>
        <asp:TextBox id="txtCookieExpire" runat="server"></asp:TextBox>
        <asp:Label id="Label4" runat="server">Less then or equal to 0 will remove the cookie </asp:Label>
        <br />
        <br />
        <asp:Button id="btnSetCookie" onclick="btnSetCookie_Click" runat="server" Text="Set Cookie"></asp:Button>
        <br />
        <br />
        <asp:Button id="btnGetCookie" onclick="btnGetCookie_Click" runat="server" Text="Get Cookie"></asp:Button>
        <br />
        <br />
        <br />
        <asp:Label id="lblCookieValue" runat="server">Label</asp:Label>
    </form>
</body>
</html>
My feeling is this Cookies are written with a Response and you have to hit the client to write a cookie.
Marty
 
Cappmgr,

Thanks, this seems to work! I've been tearing my hair out trying to get my test for cookies to work!


Cheers,

lfc77
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top