472,363 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,363 software developers and data experts.

check for cookies

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,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
4 1809
the only way to know if the broswe is accepting cookies, is to set a cookie,
send it to the browser, and have the browser post it back. on postback,
check to see if your cookie value is there, this is usually done with a
sniffer page for the site.

-- bruce (sqlwork.com)


"mike parr" <mp********@yahoo.co.uk> wrote in message
news:O6**************@TK2MSFTNGP09.phx.gbl...
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,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #2
I think you need to set the cookie on one page and check it on another page. Otherwise you may get inaccurate results, I don't think its a problem with your code. I remember I had the same issue with classic ASP ages ago and had to do the same thing to resolve it. So just split your code into two pages as shown below and it will work

[Page1.aspx

HttpCookie checkCookies = new HttpCookie("checkCookies")
checkCookies.Values["userName"] = "mike"
Response.Cookies.Add(checkCookies)
Response.Cookies["checkCookies"].Expires =DateTime.Now.AddDays(1)
Response.Redirect("Page2.aspx")

[Page2.apx]
bool acceptsCookies = false

//Notice how I have changed Request.Cookies["checkCookies"].Values["userName"] to Request.Cookies["checkCookies"] below
// Or you *may* get a System.NullReferenceException. I say *may* becuase i got it only in Mozilla 1.4 and not in IE6.0, weird
// With IE6.0 whether I blocked all cookies or not it was resolving to true, so I am not sure what is happening ther

if (Request.Cookies["checkCookies"] == null

acceptsCookies = false

els

acceptsCookies = true

//Delete test cooki
Response.Cookies["checkCookies"].Expires = DateTime.Now.AddDays(-1)
lblInfo.Text = acceptsCookies ? "Accepts cookies" : "Doesn't accept cookies"
References
http://msdn.microsoft.com/library/de...tcookies101.as

Rasik

Nov 18 '05 #3
Rasika,

I've tried what you suggested and it still returns true whether or not
cookies are enabled or disabled or my machine. I've also tried the code
in the Microsoft link, and I get the same result :

http://msdn.microsoft.com/library/de.../en-us/dv_vste
chart/html/vbtchaspnetcookies101.asp

Yet my old ASP code works fine at detecting whether cookies are enabled
or not. This is driving me crazy! Surely someone else has encountered
this problem before?
Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #4
When you say "whether or not cookies are enabled or disabled or my
machine" are you talking about disabling them in the browser? In IE?
In the Internet or Intranet zone? Because the Privacy slider used to
disable cookies in IE is only for the internet, not Intranet, which is
probably where your dev machine is located.

To disable cookies in Intranet zone, put http://localhost or
http://<machine name> into the Security Restricted Zones list.

mike parr <mp********@yahoo.co.uk> wrote in message news:<Oy**************@TK2MSFTNGP09.phx.gbl>...
Rasika,

I've tried what you suggested and it still returns true whether or not
cookies are enabled or disabled or my machine. I've also tried the code
in the Microsoft link, and I get the same result :

http://msdn.microsoft.com/library/de.../en-us/dv_vste
chart/html/vbtchaspnetcookies101.asp

Yet my old ASP code works fine at detecting whether cookies are enabled
or not. This is driving me crazy! Surely someone else has encountered
this problem before?
Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TG | last post by:
This is more of a pain than I thought it would be. I need a simple code segment to determine whether a browser accepts cookies or not. When I pass variables between pages when cookies are turned...
27
by: mrbog | last post by:
Tell me if my assertion is wrong here: The only way to prevent session hijacking is to NEVER store authentication information (such as name/password) in the session. Well, to never authenticate...
7
by: | last post by:
It is possible to check via ASP whether cookies are enabled for a person's web browser and based on this check send him to another page which does not support cookies (ie login?)? Thanks Jason
11
by: Jennifer | last post by:
Is there a way to check for to see if the user has session cookies enabled? I know how to check to see if they have cookies in general enabled, but how do you test for just session cookies? ...
0
by: Steve Johnson | last post by:
I've set IE 6.0 (in Windows XP) to block all cookies, yet Response.Browser.Cookies. still returns True. Additionally, attempts to write cookies are successful. Here's a code snippet. Does...
4
by: mike parr | last post by:
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. ...
1
by: Bobby Edward | last post by:
How do I check to see if a cookie exists or not? I tried these 3 but they don't work... If Not Request.Cookies("UserSettings")("IsAdmin") Is Nothing Then... If...
3
by: =?Utf-8?B?Qkw=?= | last post by:
if (Request.Browser.Cookies) { // Cookies supported } else { // Web browser not supports cookies }
4
by: David C | last post by:
Is there a way to check to see if the browser accepts cookies? I have the following line of code that is giving an error "Object reference not set to an instance of an object." with the following...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.