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

Cookie Question

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

Similar topics

2
by: Alexander Ross | last post by:
referring to the code below, will the cookie expire 24 hours after its created, or will it expire 24 hours after the last time I add a variable to the cookie?? if ($_POST){ if (!($_COOKIE)) ...
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
2
by: Papajo | last post by:
This is not a a PC question but just a javascript one, this script is used on webtv. The cookie on this script can be split into 3 different memory's, what I am trying to do is to be able to call...
0
by: vMike | last post by:
I don't know if I can explain this here, but I will try. I am using WebRequest/WebResponse to get product information from another website then put the selections in a shopping cart of the remote...
2
by: ashish | last post by:
We are having a web application which spans across multiple domains. Iam trying to synchronize session information between the two domains. If I know the value of ASP.NET_SessionId cookie, can I...
0
by: Alan Silver | last post by:
Hello, I am having a problem setting and resetting cookies. I'm sure I just doing something really stupid as this is such a basic issue, but I can find any answer. Please can someone help me? ...
1
by: Paul | last post by:
My site does not have a domain name, just an IP address. I set a cookie in ASP.Net and I can read it with no problem on subsequent pages. My problem is that I want to actually see the file on...
6
by: alex.kemsley | last post by:
Hi guys, I am trying to write a simple script in vain. I need it to first check to see if the cookie exists then if not write one with two variable var ref = document.referrer var page =...
10
by: DoomedLung | last post by:
Hey, I recently bought "The Javascript and DHTML Cookbook" by Danny Goodman. I'm at Chapter 1, part 1.9 "Reading and Writing Strings for Cookies" and was just interested in a utility function...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.