How to detect if cookies are enabled?

A

Agoston Bejo

Hi,
I searched around everywhere on the net, but could not find a simple example
of detecting if cookies are enabled - on server side, and without moving
from one page to another.
This should be a very basic functionality, so I am reluctant to believe that
there's no way to simply test it in a server-side script.
Anyone?

Thx,
Agoston
 
P

Patrice

AFAIK no. You have to test this client side (it could be on the same page
depending on what exactly annoys you with client side methods).

Patrice
 
M

McKirahan

Agoston Bejo said:
Hi,
I searched around everywhere on the net, but could not find a simple example
of detecting if cookies are enabled - on server side, and without moving
from one page to another.
This should be a very basic functionality, so I am reluctant to believe that
there's no way to simply test it in a server-side script.
Anyone?

Thx,
Agoston

"... detecting if cookies are enabled - on server side ..."

Are you asking how to detect if client-side cookies are enabled via
server-side ASP?


"Response.Cookies" is defined as "A collection containing the values of all
the cookies that will be sent back to the client in the current response.".

"Request.Cookies" is defined as A collection of the values of all the
cookies sent from the user's system along with their request."

Thus, I thought that this would work:

Response.Cookies("aCookie") = "Hello World"
Response.Write "Are client-side cookies enabled? = " & (Not
IsEmpty(Request.Cookies("aCookie")))

But it returned "True" even when Cookies were disabled.
 
E

Evertjan.

McKirahan wrote on 10 feb 2005 in
microsoft.public.inetserver.asp.general:
detecting if cookies are enabled - on server side,
and without moving from one page to another.

But that is a silly expectation.
Thus, I thought that this would work:

Response.Cookies("aCookie") = "Hello World"
Response.Write "Are client-side cookies enabled? = " & (Not
IsEmpty(Request.Cookies("aCookie")))

But it returned "True" even when Cookies were disabled.

How do you expect the server knowing anything about the client,
without contacting the client?

You will have to resort to something like this:

1.asp:

<%
Response.Cookies("aCookie") = "Hello World"
Response.redirect "2.asp"
%>

2.asp:

<%
If Session("aCookie") = "Hello World" Then
Response.Write "Cookies Enabled"
Else
Response.Write "Cookies Disabled"
End If
%>
 
P

Patrice

The problem is that it works as a request/response pairs.

The client sends a request and you respond to this request. If you write
down cookies to the response, it will make them visible in the *next*
request, not in the request you respond to.
You'll have to use something similar to what Evertjan shows (there is a
typo, this Cookies not Session).

You could do it on the same page if this is really a problem with some query
string to know at which step you are...

Patrice



--
 
A

Agoston Bejo

[...]
detecting if cookies are enabled - on server side,
and without moving from one page to another.

But that is a silly expectation.
[...]
How do you expect the server knowing anything about the client,
without contacting the client?

Request.ServerVariables("HTTP_USER_AGENT")
By using this, you definitely get some information about the client on
server side. So I don't think that expectation is so silly. I accept that
about cookies you cannot get information this way, however.

Thanks anyway!
 
E

Evertjan.

Agoston Bejo wrote on 12 feb 2005 in
microsoft.public.inetserver.asp.general:
[...]
detecting if cookies are enabled - on server side,
and without moving from one page to another.

But that is a silly expectation.
[...]
How do you expect the server knowing anything about the client,
without contacting the client?

Request.ServerVariables("HTTP_USER_AGENT")
By using this, you definitely get some information about the client on
server side. So I don't think that expectation is so silly. I accept
that about cookies you cannot get information this way, however.

Thanks anyway!

The HTTP_USER_AGENT value is sent with the request,
as are available cookie values tagged to the server domain.

So you could test for those cookie values, but if absent,
the difference between no cookies available and cookies switched off
cannot be detected without testing for it on the client as shown.

Yes, the powers that defined the cookie mechanism could have defined and
required a cookies-switched-off flag in the page request sent by the
browser, but they did not.
 
A

Agoston Bejo

What annoys me is that first I have to detect if JavaScript is enabled in
order to be able to test whether cookies are enabled.

Agoston
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top