Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 4th, 2004, 06:51 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default Can't delete cookie...!

I just started PHP and have looked ALL OVER the Internet for a solution to this problem, and have found lots, none of which seem to be working for me...

I have tried:

Code:
setcookie("name","",time() - 3600);
setcookie("name"," ",time() - 3600);
unset( $_COOKIE['name'] );
setcookie("name");
and other things. Please help, this problem is driving me COMPLETELY INSANE!!

Something you might want to know: when I refresh the page on which the cookie is deleted, it works. I don't have the slightest clue why.

Thank you ahead of time,

----------
---Snib---
----------
__________________
-Snib - http://www.snibworks.com
Where will you be in 100 years?
 
Old February 5th, 2004, 05:38 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

To delete a cookie you need to either make the function call without a time setting and null value, or set the time in the past. But don't forget, the cookie may not be deleted from the client machine until the browser window is closed.

Quote:
quote:from: http://www.php.net/setcookie

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.
So:
setcookie("name","",time() - 3600);
Should work, value is empty, and the time is set in the past.

setcookie("name");
Should also work, value is null.

unset() won't work because you're not passing the cookie changes via HTTP headers. You're only unsetting the variable on the server-side.

setcookie("name"," ",time() - 3600);

Won't work because the value contains a space, which, just guessing here, may not be considered NULL. But I dunno, I haven't tried it, it might work.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old February 5th, 2004, 06:10 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hello Rich,

What you say makes PERFECT sense to me! However, apparently not to PHP...:(

I tried everything again, and I also messed with all the other code around it. (I also tried setcookie("whatever",""))

I have looked around the internet, particularly at sites like this one.

This is the copy-and-paste lines that I'm using:
Code:
     setcookie("username","");
     setcookie("password","");
Please help, I've looked all over php.net...

Thanks

----------
---Snib---
----------
 
Old February 5th, 2004, 06:54 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I dunno man,
I use cookies in my site for login persistence, and this is the method that I use:

setcookie("user_id");
setcookie("password");

Which effectively does the job. If you pass it a null value, it should get rid of that cookie entry, or in the very least, render that information null and useless.

I did a little test script:

<?php

    if (isset($_GET['do_test']))
    {
        if (!isset($_COOKIE['foo']) || empty($_COOKIE['foo']))
        {
            setcookie("foo", "Hello, world!", mktime(2,22,0,2,22,2022));
            echo "Namaste!";
        }
        else
        {
            setcookie("foo");
            echo "Toasted";
        }
    }
    else if (isset($_COOKIE['foo']))
    {
        echo "I'm still here!";
    }
    else
    {
        echo "I'm gone!";
    }
?>

According to this test script, the setcookie("foo") empties the value, so $_COOKIE['foo'] still exists, but its value is null. If you close the browser window after seeing 'toasted' and reopen it the cookie is gone, deleted, if you simple reload the page with the GET parameter, you get Namaste! cause the cookie is still there, but empty. If you exclude the empty() test and just test for if !isset(), the code evaluates to 'toasted' cause it is still set.

So the best thing to do in your script to test for a cookie's value would be if isset() and !empty(), to look for deletion if !isset or empty.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old February 5th, 2004, 09:15 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Rich,

Thanks for helping me, although it still doesn't work...:(

I will let you know if I find a solution.

Thanks again,

----------
---Snib---
----------
 
Old February 6th, 2004, 07:21 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Rich,

Could I do this with a database (mySQL)?

(Perhaps this is a very basic question, I'm very new to SQL and PHP)

Thanks,

----------
---Snib---
----------
 
Old February 6th, 2004, 08:36 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Well perhaps, first it would help to know what you're trying to do, give me a little more background information...

What happens when you run the above test script?
Can you create the same output described?
Does the cookie retain a value even after calling setcookie('cookiename')
...it should still exist, but exist with NULL value until the browser is closed, at which time it should be deleted.

If this isn't working for you then the problem may relate to a browser setting..
Does the domain name creating the cookie match the domain name trying to modify it?
Is the domain a third party domain?

And lastly, what is the information going to be used for?

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old February 6th, 2004, 09:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not to hijack your thread, but there's something that's fundamentally missing from the description of the problem --

when you call setcookie() to delete the cookie, are you checking to see if the cookie still exists in the same script? Because if you are, it's guaranteed to exist.

A web browser (the client) will send ALL relevant cookie information to the server when it makes an HTTP request. This means that $_COOKIE is set and exists before your script begins execution.

Deleting that cookie with setcookie() will send the appropriate HTTP headers back with the response when the response is sent, but it doesn't affect the existence or the contents of $_COOKIE for the currently executing script.

I mention this because you say:

Quote:
quote:Originally posted by Snib
Something you might want to know: when I refresh the page on which the cookie is deleted, it works. I don't have the slightest clue why.
Which is exactly how you should expect things to work. When you refresh the page, the client (the web browser) is sending a new HTTP request to the web server, but this time it's NOT sending the cookie information with the request, since the cookie is expired. This is why the PHP script (on the server) doesn't think the cookie exists after a page refresh -- because it doesn't.

It's all a matter of timing. The state of your PHP script is determined when the client makes its request. If you modify or delete the cookie data in a script, those changes don't take effect until the client sends another request with the updated cookie data.

Another related issue -- if you set the variable "foo" in a cookie to the value "Hello, world", then $_COOKIE['foo'] doesn't automatically exist yet. It doesn't exist until the client makes a new request with the new cookie variable in place.

For this reason, $_COOKIE and $_SESSION perform fundamentally different roles -- $_COOKIE is meant to be a READ ONLY variable. You can't create or modify cookie variables by manipulating $_COOKIE -- you need to call setcookie() or header() to do that. In contrast, $_SESSION is a READ/WRITE variable.

This is because session data is ***NOT*** stored on the client machine!! Most people don't understand this fact about sessions. Sure, sessions can USE cookies behind the scenes, but the only session-related data that is stored on the client side in a cookie is the session ID. This is why you have to call session_start() before you send any output to the client. session_start() creates or updates a cookie variable that stores the session ID.

When a script begins, PHP looks for the session ID in the cookie variables. If it exists, it opens the data file on THE SERVER session.save_path directory that stores the variables associated with the session ID, and populates $_SESSION with the data from that file.

You can modify the $_SESSION array all you want during your script. When your script terminates, PHP will write all your session variables from $_SESSION back into the datafile identified by your session ID.

Hope this helps clear things up.


Take care,

Nik
http://www.bigaction.org/
 
Old February 7th, 2004, 11:35 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hello,

Thanks a lot for your help guys.

I learned about sessions and used them and now everything works great.

Thanks again for your help,

----------
---Snib---
----------
 
Old July 15th, 2007, 10:32 PM
Registered User
 
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have just had this same problem but managed to solve it by specifying the path where the cookie is stored. If the PHP file that is making the operation on the COOKIE isn't in the same directory you won't be able to write to it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to delete file System.IO.Delete error maricar C# 13 March 14th, 2014 06:50 AM
php/mysql delete button and delete query dungey PHP Databases 17 April 11th, 2009 12:24 PM
How Can I Delete Cookie files. MICZ VB How-To 4 August 18th, 2007 04:56 AM
how to delete a row when click delete hyperlink naveenkumarg1 Pro JSP 1 August 16th, 2004 01:29 AM
How Can I Delete Cookie files. MICZ Pro VB 6 2 August 13th, 2004 10:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.