473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1155
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: wrytat | last post by:
How do you check in the vb codes whether the user has enabled the option to accept cookies in his web browser? Thank you in advance.
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.