473,508 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1890
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
3175
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
7086
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
5554
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
6023
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
1283
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
1159
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
8899
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
7772
by: =?Utf-8?B?Qkw=?= | last post by:
if (Request.Browser.Cookies) { // Cookies supported } else { // Web browser not supports cookies }
4
2266
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
7226
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
7125
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
7328
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,...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.