Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect if a user has cookies turned off 2

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
Hey all, what are some of the ways to detect if a user has cookies turned off and display a user friendly message? Can the solution be HTML based or does it need to be server-code based? At work we're using JSP (I'm a Coldfusion developer, btw) but i'm helping them investigate this issue and propose possible solutions. Any help is appreciated.

____________________________________
Just Imagine.
 
That can neither be server-code based nor HTML based. In order to see if a person has cookies turned on or off, your program will have to gain access to their machine to see the settings.

The only way I can think of how that would be done is with an Active X object.

(I'm sure there are other ways, but I know it can't be done through HTML or server-side code.)

[monkey][snake] <.
 
Hi

You will need some code. For client-side this will do it, but you know, JavaScript can also be turned off.
Code:
<script type="text/javascript">
document.cookie=[i]'cookietest=ok'[/i];
[b]if[/b] (document.cookie) document.cookie=[i]'cookietest=ok;expires=Sat, 31 Jan 1970 00:00:00 GMT'[/i];
[b]else[/b] document.writeln([i]'Dear visitor. You have the cookies turned off.'[/i]);
</script>

Feherke.
 
me said:
That can neither be server-code based nor HTML based. In order to see if a person has cookies turned on or off, your program will have to gain access to their machine to see the settings.

I'm being stupid and I stand corrected.


[monkey][snake] <.
 
Yeah of course you can look at cookies on a user's computer that's what a cookie basically is. So yeah, attempt to put a cookie on a user's machine, then try to grab that cookie, if there's not one there to grab, you can assume they have cookies disabled.



[monkey][snake] <.
 
OK, I'm having some problems when I run a test. This is how I am doing this:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

<script type="text/javascript">
document.cookie='cookietest=ok';
if (document.cookie) document.cookie='cookietest=ok;expires=Sat, 31 Jan 2010 00:00:00 GMT';
else document.write('Dear visitor. You have the cookies turned off.');
</script>
</head>
<body>
</body>
</html>

I have cookies turned off in IE6 but whenever I load this page I get nothing and I still the cookie being created.



____________________________________
Just Imagine.
 
Hi

I think first I will explain it.
Code:
[gray]// set a test cookie. without date it will be session cookie[/gray]
document.cookie=[i]'cookietest=ok'[/i];
[gray]// test any cookie exists[/gray]
[b]if[/b] (document.cookie)
[gray]// if yes,[/gray] [red]remove the cookie by setting its expire date in the [u]past[/u][/red]
document.cookie=[i]'cookietest=ok;expires=Sat, 31 Jan [red]2010[/red] 00:00:00 GMT';[/i]
[gray]// otherwise display an error message[/gray]
[b]else[/b] document.write([i]'Dear visitor. You have the cookies turned off.'[/i]);
If your question still stands after you will correct the expire date, then probably you want a more strict checking of the cookie
Code:
[b]if[/b] (document.cookie.indexOf('cookietest=ok')!=-1) [gray]...[/gray]
If neither that gives what you want, then better check the BrowserSpy page I mentioned earlier, for an exact cookie check. Although I can only barely imagine situation when the modified [tt]if[/tt] condition would not be enough.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top