473,569 Members | 2,436 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(objec t sender, System.EventArg s e)
{
if (!(Page.IsPostB ack))
{
//check browser accepts cookies
HttpCookie checkCookies = new HttpCookie("che ckCookies");
checkCookies.Va lues["userName"] = "mike";
Response.Cookie s.Add(checkCook ies);

bool acceptsCookies = false;

if (Request.Cookie s["checkCooki es"].Values["userName"] == null)
{
acceptsCookies = false;
}
else
{
acceptsCookies = true;

//Delete test cookie
Response.Cookie s["checkCooki es"].Expires =
DateTime.Now.Ad dDays(-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 1163
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********@yah oo.co.uk> wrote in message
news:O6******** ******@TK2MSFTN GP09.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(objec t sender, System.EventArg s e)
{
if (!(Page.IsPostB ack))
{
//check browser accepts cookies
HttpCookie checkCookies = new HttpCookie("che ckCookies");
checkCookies.Va lues["userName"] = "mike";
Response.Cookie s.Add(checkCook ies);

bool acceptsCookies = false;

if (Request.Cookie s["checkCooki es"].Values["userName"] == null)
{
acceptsCookies = false;
}
else
{
acceptsCookies = true;

//Delete test cookie
Response.Cookie s["checkCooki es"].Expires =
DateTime.Now.Ad dDays(-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("che ckCookies")
checkCookies.Va lues["userName"] = "mike"
Response.Cookie s.Add(checkCook ies)
Response.Cookie s["checkCooki es"].Expires =DateTime.Now.A ddDays(1)
Response.Redire ct("Page2.aspx" )

[Page2.apx]
bool acceptsCookies = false

//Notice how I have changed Request.Cookies["checkCooki es"].Values["userName"] to Request.Cookies["checkCooki es"] below
// Or you *may* get a System.NullRefe renceException. 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.Cookie s["checkCooki es"] == null

acceptsCookies = false

els

acceptsCookies = true

//Delete test cooki
Response.Cookie s["checkCooki es"].Expires = DateTime.Now.Ad dDays(-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/vbtchaspnetcook ies101.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********@yah oo.co.uk> wrote in message news:<Oy******* *******@TK2MSFT NGP09.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/vbtchaspnetcook ies101.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
3178
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 off, the global variables are empty between pages, fine, that tells me cookies store the global variables - right? I store the values in $_session and...
27
7093
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 a user from information you got from the session. Each secure app on a site must challenge the user for name and password, each and every time the...
7
5562
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
6029
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? Thanks Jen
0
1286
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 anyone see an error? Something I'm missing? Steve Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
4
1893
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. Can anybody help me out with this? private void Page_Load(object sender, System.EventArgs e) { if (!(Page.IsPostBack))
1
1824
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
8903
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 Request.Cookies("UserSettings")("IsAdmin") IsNot Nothing Then... If Not IsNothing(Request.Cookies("UserSettings")("IsAdmin")) Then... They all give me a...
3
7775
by: =?Utf-8?B?Qkw=?= | last post by:
if (Request.Browser.Cookies) { // Cookies supported } else { // Web browser not supports cookies }
4
2267
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 line: If Server.HtmlEncode(Request.Cookies("level").Value) = "admin" Then Thanks. David
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
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 we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.