473,396 Members | 2,013 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,396 software developers and data experts.

How can I confirm a cookie is accepted?

I'm busy rewriting by F1 database and I want to use cookies to store
various user definable views, (basically so that when the visitor
returns to a specific page, that page will redisplay in the format that
visitor saved). Problem is, I don't know if the cookie was accepted or not.

setcook() returns TRUE if it worked but that doesn't indicate whether or
not it was accepted.

I tried the following:

/* $maskname = Name of the PHP 'cos there are different */
/* PHPs with different keep values */
/* $keepthis = A string indicating what fields to hide */
/* or display */
/* $keepdate = now() + 1 year so that it doesn't */
/* disappear when the visitor stops browsing */
setcookie($maskname, $keepthis, $keepdate);
$chckcook = $_COOKIE[$maskname];
if ($chckcook != $keepthis) {
$cookwork = false;
} else {
$cookwork = true;
} /* End of code portion */

But it always returns $cookwork = false. Where I'm going wrong?

The <html> tag is after the above bit of code although there are blank
lines, comments and other code between the <?php and this bit. I'm using
PHP Version 4.1.1 on a Win/2000 machine for development.

Basically, all I want to do is confirm that the cookie was written and
if not, then print up a message to tell the visitor that the cookie
wasn't stored so his carefully chosen format is going to disappear
unless he accepts the cookie.

MfG

Geoff.

--
Unofficial F1 Database: http://glibs.ssmmdd.co.uk/
Update: 11th July, 2004
USENET Email address is a spam trap, send Emails to address in the DB
Jul 17 '05 #1
4 2621
Geoff May wrote:
$chckcook = $_COOKIE[$maskname];
if ($chckcook != $keepthis) {
$cookwork = false;
} else {
$cookwork = true;
} /* End of code portion */

But it always returns $cookwork = false. Where I'm going wrong?
From the manual at :
http://www.php.net/manual/en/function.setcookie.php

Cookies will not become visible until the next loading of a page that
the cookie should be visible for. To test if a cookie was successfully
set, check for the cookie on a next loading page before the cookie expires.
The <html> tag is after the above bit of code although there are blank
lines, comments and other code between the <?php and this bit. I'm using
PHP Version 4.1.1 on a Win/2000 machine for development. As long as there is no white space above the opening <?php tag you will
be OK. If there is, you will get a 'headers already sent' error
Basically, all I want to do is confirm that the cookie was written and
if not, then print up a message to tell the visitor that the cookie
wasn't stored so his carefully chosen format is going to disappear
unless he accepts the cookie.

Redirect the browser to the next page and check the existance of the
cookie. If the cookie is not there, display your warning, otherwise
display the page as intended. An intermediate page may be a good idea.

Bear in mind that if the browser is set to reject cookies, it will be
too late to attempt to reset it on the next page.

HTH, regards,
Andy
Jul 17 '05 #2

"Geoff May" <Be***********@t-online.de> wrote in message
news:cd*************@news.t-online.com...
I'm busy rewriting by F1 database and I want to use cookies to store
various user definable views, (basically so that when the visitor
returns to a specific page, that page will redisplay in the format that
visitor saved). Problem is, I don't know if the cookie was accepted or not.
setcook() returns TRUE if it worked but that doesn't indicate whether or
not it was accepted.

I tried the following:

/* $maskname = Name of the PHP 'cos there are different */
/* PHPs with different keep values */
/* $keepthis = A string indicating what fields to hide */
/* or display */
/* $keepdate = now() + 1 year so that it doesn't */
/* disappear when the visitor stops browsing */
setcookie($maskname, $keepthis, $keepdate);
$chckcook = $_COOKIE[$maskname];
if ($chckcook != $keepthis) {
$cookwork = false;
} else {
$cookwork = true;
} /* End of code portion */

But it always returns $cookwork = false. Where I'm going wrong?

The <html> tag is after the above bit of code although there are blank
lines, comments and other code between the <?php and this bit. I'm using
PHP Version 4.1.1 on a Win/2000 machine for development.

Basically, all I want to do is confirm that the cookie was written and
if not, then print up a message to tell the visitor that the cookie
wasn't stored so his carefully chosen format is going to disappear
unless he accepts the cookie.

MfG


You might consider setting a test cookie *before* you get to the page where
they select their format. Don't allow them to go through the motions of
selecting a format if the cookis aren't supported. Check for the test cookie
before offering the selection.

- Virgil
Jul 17 '05 #3
Geoff May wrote:
I'm busy rewriting by F1 database and I want to use cookies to store
various user definable views, (basically so that when the visitor
returns to a specific page, that page will redisplay in the format that
visitor saved). Problem is, I don't know if the cookie was accepted or not.

setcook() returns TRUE if it worked but that doesn't indicate whether or
not it was accepted.

I tried the following:

/* $maskname = Name of the PHP 'cos there are different */
/* PHPs with different keep values */
/* $keepthis = A string indicating what fields to hide */
/* or display */
/* $keepdate = now() + 1 year so that it doesn't */
/* disappear when the visitor stops browsing */
setcookie($maskname, $keepthis, $keepdate);
$chckcook = $_COOKIE[$maskname];
if ($chckcook != $keepthis) {
$cookwork = false;
} else {
$cookwork = true;
} /* End of code portion */

But it always returns $cookwork = false. Where I'm going wrong?

The <html> tag is after the above bit of code although there are blank
lines, comments and other code between the <?php and this bit. I'm using
PHP Version 4.1.1 on a Win/2000 machine for development.

Basically, all I want to do is confirm that the cookie was written and
if not, then print up a message to tell the visitor that the cookie
wasn't stored so his carefully chosen format is going to disappear
unless he accepts the cookie.

MfG

Geoff.


Since you cannot guarantee that the browser has cookies enabled, - especially if
it is on the web (external to your site where you have no control), using
cookies may not be the right answer. If you are having them log in - and
possibily using the database as the repository, why not load that data into the
database and restore from there. This would be server-side and not dependent
upon the browser. Also look at sessions to see if the session is valid or not
(expire after 1 hour for example) The docs and google are repleat with examples
of how to do all of this.

--
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #4
Michael Austin wrote:
Geoff May wrote:
[snipped]


Since you cannot guarantee that the browser has cookies enabled, -
especially if it is on the web (external to your site where you have no
control), using cookies may not be the right answer. If you are having
them log in - and possibily using the database as the repository, why
not load that data into the database and restore from there. This would
be server-side and not dependent upon the browser. Also look at
sessions to see if the session is valid or not (expire after 1 hour for
example) The docs and google are repleat with examples of how to do all
of this.


I did consider that option yesterday but I'd rather not have users
having to log in. I suppose I could change the system so that if they
want to save, they have to log in else they have to put up with "system
defaults".

At the moment, I have stuck in a comment in my notes page to say that
the database doesn't check that the cookie is accepted so if cookies are
disabled, tough. I phrased it more politely ;-)

Oh well, thanks to everyone who replied, you have been most helpful.

MfG

Geoff.

--
Unofficial F1 Database: http://glibs.ssmmdd.co.uk/
Update: 11th July, 2004
USENET Email address is a spam trap, send Emails to address in the DB
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: The Lone Wolf | last post by:
Hi, if i try to set a cookie but the user has cookies disabled does this resolve in an error? if so how to prevend it? thanks
3
by: HorseGeek | last post by:
I can't find a cookie that my code is writing. The behavior of my webpages indicates that the cookie IS being written SOMEPLACE. However, I can't find it. My client does not want the code going...
4
by: elwis.tsapzang | last post by:
hello. I decide to use forms authentication in my web site. I do some test in my own network. I choose not to allow any cookie. But my form still work althought I don't allow any cookie. My site...
3
by: rodchar | last post by:
Hey all, I'm trying to gain more knowledge about state management and I found an MSDN Help article about cookies. I posted the short sample below. From what I read in the article if you do the...
1
by: BJörn Lindqvist | last post by:
Hello, I have some very serious trouble getting cookes to work. After a lot of work (urllib2 is severly underdocumented, arcane and overengineerd btw) I'm finally able to accept cookes from a...
6
by: Michi Henning | last post by:
I'm running the following code in Safari 2.0.4: document.cookie = 'MyCookie=1'; if(document.cookie == '') alert('document.cookie is empty!'); document.cookie always returns the empty string,...
6
by: Alessandro Fachin | last post by:
Hi, i am trying to forge a new cookie by own with cookielib. But i don't still have success. This a simply code: import cookielib, urllib, urllib2 login = 'Ia am a cookie!' cookiejar =...
2
by: Sean | last post by:
I am trying to read a cookie I set but I am not sure if I really set it correctly or I am not reading it correctly. I was given the following instructions to set the cookie. It appears to be...
3
by: TimSki | last post by:
Hi, I have the following javascript function which creates a cookie function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays);...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.