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

How can I confirm a cookie is accepted?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.