473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Check for cookies

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
5 3165
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
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
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...
7
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
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? ...
1
by: rsindall | last post by:
Hi I have some code for checking for cookies that sets on page1 and checks on page2. and its based on the code my MS ...
0
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...
4
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. ...
1
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...
3
by: =?Utf-8?B?Qkw=?= | last post by:
if (Request.Browser.Cookies) { // Cookies supported } else { // Web browser not supports cookies }
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.