473,799 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking cookies

Bob Ross
119 New Member
I'm sure I know the answer to this already but....
Does anyone know a good way of checking if cookies are enabled on a browser?

I currently have
Expand|Select|Wrap|Line Numbers
  1.  
  2. If Request.QueryString("Trying") = "" Then
  3.          Session.Add("SessionCookie", "ON")
  4.          Response.Redirect(Request.Path & "?Trying=ON", True)
  5. Else
  6.          If Session("SessionCookie") = "" Then
  7.                   Cookieslabel.Visible = True
  8.          End If
  9.          Session.Add("SessionCookie", "")
  10. End If
But was wondering if there was a better way to do it? Preferably without a postback.
Feb 14 '07 #1
4 1321
Frinavale
9,735 Recognized Expert Moderator Expert
I'm sure I know the answer to this already but....
Does anyone know a good way of checking if cookies are enabled on a browser?

I currently have
Expand|Select|Wrap|Line Numbers
  1.  
  2. If Request.QueryString("Trying") = "" Then
  3.          Session.Add("SessionCookie", "ON")
  4.          Response.Redirect(Request.Path & "?Trying=ON", True)
  5. Else
  6.          If Session("SessionCookie") = "" Then
  7.                   Cookieslabel.Visible = True
  8.          End If
  9.          Session.Add("SessionCookie", "")
  10. End If
But was wondering if there was a better way to do it? Preferably without a postback.

I haven't yet found a solution that doesn't post back and I doubt that there is one. I think the only way to check if cookies are enable is to create one and then check if it is still there after posting back.

I've looked for countless hours for the same thing.
I too would love it if someone would share a solution that doesn't post back.

Just don't get your hopes up,

-Frinny
Feb 14 '07 #2
kenobewan
4,871 Recognized Expert Specialist
To avoid postback I believe that you could use client-side script. In the past I have checked cookies using JavaScript, but if you have to do it server-side I hope that this article may help:
Client Script in ASP.NET Web Pages
Remember that you can use not Page.IsPostBack to avoid postbacks server side...
Feb 15 '07 #3
Frinavale
9,735 Recognized Expert Moderator Expert
To avoid postback I believe that you could use client-side script. In the past I have checked cookies using JavaScript, but if you have to do it server-side I hope that this article may help:
Client Script in ASP.NET Web Pages
Remember that you can use not Page.IsPostBack to avoid postbacks server side...
Right now I have a javaScript script that checks to see if Cookies and JavaScript are enabled.

The way I solved this was to have my .NET application register my javaScript code using the ClientScript class in my application's preLoad() function. This way the cookies&javascr ipt check is done before the page is ever loaded.

This script posts back with the true/false values indicating if javaScript and Cookies are enabled. Then my server side logic the displays the appropriate web user control based on these findings.

It was tricky and involved implementing the IPostBackDataHa ndler.

How were you able to do this without posting back to the server?

-Frinny
Feb 15 '07 #4
Bob Ross
119 New Member
Thanks for everyones help.
I managed to get it working without postback by using

Expand|Select|Wrap|Line Numbers
  1. var cookieEnabled=(navigator.cookieEnabled)? true : false
  2.  
  3. //if not IE4+ nor NS6+
  4. if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
  5. document.cookie="testcookie"
  6. cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
  7. }
  8.  
  9. //if (cookieEnabled) //if cookies are enabled on client's browser
  10. //do whatever
Feb 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3886
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any new site I goto seems to prompt me to store their cookie. The other is Pocket IE on Pocket PC 2002, with the cookies set to 'enabled'. My problem is that the cookies dont seem to be being written with my ASP. I dont get the prompt to store...
2
1783
by: Andrew Duffy | last post by:
Hi, I just want to check that i'm not going crazy.. I want to check whether or not a cookie has already been set and so I am using the following code. cookieValue = Request.Cookies("CartID") 'if cookie hasn't already been set, then set it! If cookieValue = "" Then
0
333
by: Lucifer | last post by:
Hi I have some code for checking for cookies, that sets a cookie on page1 and checks for it on page2. and its based on the code by MS: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchaspnetcookies101.asp under section: 'Checking Whether a Browser Accepts Cookies' This code works great, when its in my development enviroment.
2
2031
by: CES | last post by:
I'm sorry for the 101 question but I can't seem to get this to work... I'm tiring to test if a cookie has been created before. Everything I've seen indicates that Request.Cookies works the same in .net as it did in old asp but for some reason I'm getting "System.NullReferenceException: Object reference not set to an instance of an object" when ever I attempt to test for the cookie . It doesn't return an error if the cookie exists but...
3
1359
by: Ollie | last post by:
Can you check whether a users browser allows you to use cookies? Cheers Ollie
1
1118
by: Tina | last post by:
When I use Request.Browser.Cookies I always get true returned. Even when the browser has cookie acceptance turned off. Why? Thanks, T
0
905
by: beaker | last post by:
Hello, this is my first foray into the world of web programming, and none of the searching I've done to solve this problem has left me any the wiser, so I hope someone out there can help... What I'm trying to do: There is a turn based strategy game I play that has a web interface. I want to write a tool to check my status webpage every so often to see if I have new turns to play, and go 'beep' and briefly show a pop up if I do - just
4
4051
by: Jeff | last post by:
I have a vb.net application (2005) requiring session variables and want to test to make certain that the user's cookies are enabled. I can set a test session variable on one page and attempt to read it on the next with the code below in order to determine if the user has their browser's cookies enabled, but is there a good way to do this on the first/default page without a redirect to another page that will actually do the test? Thanks
2
3801
by: =?Utf-8?B?R2FpbFM=?= | last post by:
I tried to post this last week, but never saw it, so I hope it's not a duplicate. (Sorry if it is.) I am rewriting our member log-in page in classic ASP from PHP. We will be switching platforms next month. Every member has an access number to log in. Once they have logged in, if they accept it, a cookie is provided so they do not have to log in again. When the code is supplied via the cookie or form, it is validated against a database...
0
9688
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10031
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7571
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2941
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.