473,387 Members | 1,516 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,387 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 3332
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.