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

Cookie Question

P: n/a
I wrote a small .php script that creates a cookie on my local PC - I
also wrote one that deletes it.

My question is this: when the cookie has been created and I run the
delete .php script, when it first loads it does not delete the cookie
- I always have to click on the browser refresh button - why doesn't
the script execute on the first execution?

Thanks...

Jul 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I'd suggest you have a further read on cookies and how they relate to
intricacies of HTTP. It can get a bit tricky ;).

Anyway. The PHP script wouldn't 'delete' the cookie from your computer,
it simply sends a header to your browser stating that the cookie has
expired, and it is then up to the browser to delete that cookie from
_then on after_.

So usually once you have sent a cookie that will expire the current
cookie (read: setting a cookie of the same name, but a past expiry date):

setcookie('mycookie', '', mktime()- 31536000, $cookiepath,
$cookiedomain, $cookiesecure);

You will then need to instruct the browser to refresh the page, by
sending a location header:

//
// Time to refresh!
//
header ('Location: index.php');

Hope this helps,
Grant.

Ralph Freshour wrote:
I wrote a small .php script that creates a cookie on my local PC - I
also wrote one that deletes it.

My question is this: when the cookie has been created and I run the
delete .php script, when it first loads it does not delete the cookie
- I always have to click on the browser refresh button - why doesn't
the script execute on the first execution?

Thanks...


Jul 16 '05 #2

P: n/a
Ralph Freshour <ra***@primemail.com> wrote in message news:<lu********************************@4ax.com>. ..
I wrote a small .php script that creates a cookie on my local PC - I
also wrote one that deletes it.

My question is this: when the cookie has been created and I run the
delete .php script, when it first loads it does not delete the cookie
- I always have to click on the browser refresh button - why doesn't
the script execute on the first execution?


http://in2.php.net/set_cookie

Common Pitfalls:
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. Expire time is set via the expire parameter. A nice way to
debug the existence of cookies is by simply calling
print_r($_COOKIE);.

Cookies must be deleted with the same parameters as they were set
with. If the value argument is an empty string (""), and all other
arguments match a previous call to setcookie, then the cookie with the
specified name will be deleted from the remote client.

Cookies names can be set as array names and will be available to your
PHP scripts as arrays but seperate cookies are stored on the users
system. Consider explode() or serialize() to set one cookie with
multiple names and values.


---
Email: rrjanbiah-Y!com
Jul 16 '05 #3

P: n/a
Yes that helped.

I have a form where I use a checkbox to have the user inform me if
they want to use a cookie to control color of the web page - if they
enable the checkbox where is it that I can set the cookie using php?
In the submitted form? I have tried the submitted form but I get an
error saying headers have already been sent!

Your right, this is a bit tricky!!!
On Sat, 09 Aug 2003 17:45:34 +1000, John Smith
<un******@unleadedonline.net.nospamthanks> wrote:
I'd suggest you have a further read on cookies and how they relate to
intricacies of HTTP. It can get a bit tricky ;).

Anyway. The PHP script wouldn't 'delete' the cookie from your computer,
it simply sends a header to your browser stating that the cookie has
expired, and it is then up to the browser to delete that cookie from
_then on after_.

So usually once you have sent a cookie that will expire the current
cookie (read: setting a cookie of the same name, but a past expiry date):

setcookie('mycookie', '', mktime()- 31536000, $cookiepath,
$cookiedomain, $cookiesecure);

You will then need to instruct the browser to refresh the page, by
sending a location header:

//
// Time to refresh!
//
header ('Location: index.php');

Hope this helps,
Grant.

Ralph Freshour wrote:
I wrote a small .php script that creates a cookie on my local PC - I
also wrote one that deletes it.

My question is this: when the cookie has been created and I run the
delete .php script, when it first loads it does not delete the cookie
- I always have to click on the browser refresh button - why doesn't
the script execute on the first execution?

Thanks...


Jul 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.