-4

I have created one php web page which creates a cookie. That web page redirects the user on another (second) php web page. On this second web page I'm trying to delete the cookie which is created by the first page. But cookie is not getting deleted. And the second web page shows an error like "can not modify header information"

My php code format for deleting that cookie is like:

if(isset($_COOKIE['cookieName']))
{
setCookie('cookieName','values',time()-3600,'/','example@domain.com',0);
}
jszobody
  • 28,495
  • 6
  • 61
  • 72
mack
  • 24
  • 4
  • Please read the error message carefully. It clearly explains why and where your `setcookie` call is failing. (And the answer to your question is, of course, "yes"—cookies are not read-only.) – Álvaro González Nov 18 '13 at 16:53

3 Answers3

0

I hope you are making use of unset()

Do like this

if(isset($_COOKIE['cookieName']))
{
unset($_COOKIE['cookieName']));
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

Setting cookies is done in the HTTP header. This header is sent before the actual content of the page. As a result, you can only (un)set the cookie of you have not yet sent any output.

This is also stated in the setcookie documentation:

Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

For example:

<?php
    if (isset($_COOKIE['cookieName'])) {
        unset($_COOKIE['cookieName']);
        setcookie("cookieName", "", time()-3600);
    }
?>
<html>
  ....
</html>

(Also see the question Remove a cookie.)

Community
  • 1
  • 1
Mark van Lent
  • 12,641
  • 4
  • 30
  • 52
0

Can you try this,

 unset($_COOKIE['cookieName']);
 setcookie('cookieName', null, -1, '/');

Path: The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

Domain: The domain that the cookie is available to. Setting the domain to 'www.example.com' will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as 'example.com' will be available to higher subdomains, such as 'www.example.com'.

Krish R
  • 22,583
  • 7
  • 50
  • 59