By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,495 Members | 1,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,495 IT Pros & Developers. It's quick & easy.

Check for cookies

P: n/a
TG
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 when cookies are
turned on values are passed between web pages - I can see these values in
fields that are displayed in the next web page.

I need to be able to check on the 'first' entry webpage after a user enters
that page directly whether or not their browser accepts cookies at that
point. Everything I have tried complex to simiple won't work. How do I do
this?
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
TG wrote:
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 when cookies are
turned on values are passed between web pages - I can see these values in
fields that are displayed in the next web page.

I need to be able to check on the 'first' entry webpage after a user enters
that page directly whether or not their browser accepts cookies at that
point. Everything I have tried complex to simiple won't work. How do I do
this?


You could use PHP or JavaScript but unfortunately because of different
issues with different browsers there is no method to tell you straight
off the bat if cookies are enabled or disabled... Each browser has to
provide something in its header (see phpinfo(); output help page and
test it with your browser).

But... Perhaps I am misunderstanding you, but are you saying that your
cookies do not appear until you use sessions?

Cookies and sessions are not related - They can provide similar
functionality, but you do not need one to have the other.

Cookies I believe work by createing values, either in a small temporary
file or in memory (pending if they have an expiry time or not).

Sessions I believe work by extending your URL in the address bar with
something cryptic that will correspond to a temporary file held on the
server. I believe sessions should work across all browsers, or at least
more so than cookie based transactions.

Your cookie is only visable with a browser refresh thus the cookie you
might have set earlier (before you started to use sessions) has probably
become visable to you after a browser refresh - It has nothing to do
with any sessions being switched used, or not.

Try the following simple test:

<?
if(count($_COOKIE)>0)
{ print("Our cookies are set<br>");
foreach($_COOKIE as $cookieName=>$cookieValue)
{ print("<br>$cookieName = $cookieValue");
}
}
$random=mt_rand();
set_cookie("random","$random");
print("<br>Current random = $random");
print("<br>Last random = $_COOKIE[random]");
?>

Using the above code, the loop and message "Our cookies are set" should
be displayed at the second refresh of your browser.

Also, the Current random should always differ "Last random" value.

If you want to use cookies, and you want to check that the cookie you
set, then have your first page set a cookie value from the onset of its
display - You could force a browser refresh or re-direct to another page
and then check if your cookie is available or not... its a solution, but
not an environmentally friendly one...

Does that help you in any way?

Jul 17 '05 #2

P: n/a
You can use JavaScript to tell whether there's a cookie or not in the first
page:

<script>

while(document.cookie == null || document.cookie.length == 0) {
alert('You must enable cookie!');
}

</script>

If the page starts a session, then there should be one.

Uzytkownik "TG" <tg********@cox.net> napisal w wiadomosci
news:TDAZb.13810$Dc2.8427@lakeread01...
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 when cookies are
turned on values are passed between web pages - I can see these values in
fields that are displayed in the next web page.

I need to be able to check on the 'first' entry webpage after a user enters that page directly whether or not their browser accepts cookies at that
point. Everything I have tried complex to simiple won't work. How do I do
this?

Jul 17 '05 #3

P: n/a
TG
Thanks!

That worked perfectly.

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:8L********************@comcast.com...
You can use JavaScript to tell whether there's a cookie or not in the first page:

<script>

while(document.cookie == null || document.cookie.length == 0) {
alert('You must enable cookie!');
}

</script>

If the page starts a session, then there should be one.

Uzytkownik "TG" <tg********@cox.net> napisal w wiadomosci
news:TDAZb.13810$Dc2.8427@lakeread01...
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 when cookies are
turned on values are passed between web pages - I can see these values in fields that are displayed in the next web page.

I need to be able to check on the 'first' entry webpage after a user

enters
that page directly whether or not their browser accepts cookies at that
point. Everything I have tried complex to simiple won't work. How do I do this?


Jul 17 '05 #4

P: n/a
"TG" <tg********@cox.net> wrote in message news:<TDAZb.13810$Dc2.8427@lakeread01>...
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 when cookies are
turned on values are passed between web pages - I can see these values in
fields that are displayed in the next web page.

I need to be able to check on the 'first' entry webpage after a user enters
that page directly whether or not their browser accepts cookies at that
point. Everything I have tried complex to simiple won't work. How do I do
this?


Manual & usernotes are your friends <http://in2.php.net/setcookie>

--
"Success is not what you achieve, but it is what you die for"
If you live in USA, please support John Edwards.
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<8L********************@comcast.com>...
You can use JavaScript to tell whether there's a cookie or not in the first
page:

<script>

while(document.cookie == null || document.cookie.length == 0) {
alert('You must enable cookie!');
}

</script>

If the page starts a session, then there should be one.


Why? How? :-/

--
"Success is not what you achieve, but it is what you die for"
If you live in USA, please support John Edwards.
Email: rrjanbiah-at-Y!com
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.