Log in

View Full Version : Delete Cookie



bluewalrus
02-11-2010, 02:45 AM
How can I delete a cookie rather than the value of the cookie?

I tried this



unlink($_COOKIE["Gallery"]);


But that's bringing up


unlink([COOKIE_VALUE]) [function.unlink]: No such file or directory

Nile
02-11-2010, 02:51 AM
Try unset(). Unlink will get rid of the file path with that name.

djr33
02-11-2010, 03:02 AM
What? Why are cookies related to files?

You cannot delete a cookie. Instead, you set it to a blank value and put the time in the past so that the browser removes it soon. Setting $_COOKIE[] is only for the current execution of the script, but if you delete the cookie removing the entry from $_COOKIE[] will also help remove confusion.
See example 2:
http://www.php.net/manual/en/function.setcookie.php

bluewalrus
02-11-2010, 03:27 AM
I'm trying to use a cookie to store a directory name, the director(y)/(ies) are going to act like a gallery. I think i'm doing this a long, inefficient, and drawn out way...


<?php
if (isset($_GET['clear'])) {
if ($_COOKIE["Gallery"] == $_GET['clear']) {
setcookie ("Gallery", "", time() - 3600);
unset($_COOKIE["Gallery"]);
unset($_POST['gallery']);
unset($_POST['gal_is']);
}
}
// $user is determined earlier with password
$path = "./images/$user/";
if ((isset($_POST['gallery']) && ($_POST['gallery'] != "")) || (isset($_POST['gal_is']) && ($_POST['gal_is'] !=""))) {
if (isset($_POST['gallery'])) {
$gal_name = $_POST['gallery'];
if (file_exists($path . $gal_name)) {
echo "<script type=\"text/javascript\">\nalert('Gallery Already Exists');\n</script>\n";
} else {
mkdir($path . $gal_name, 0777);
mkdir($path . $gal_name . "/thumb", 0777);
}
$gal = $_POST['gallery'];
} else {
$gal = $_POST['gal_is'];
}
setcookie ("Gallery", $gal);
}
if (isset($_COOKIE['Gallery'])) {
$gal_set = $_COOKIE["Gallery"];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if (!isset($gal_set)) {
?>
<form action="?" method="post" onsubmit="if (document.New_Gal.Gal_Is.value != '') { document.New_Gal.submit } else { alert('Enter a Name');}" name="New_Gal">
Create a New Gallery:<br /><input type="gal" name="gallery" type="text" /><br />
Select an Exisiting Gallery:<br />
<?php
$path = "./images/$user/";
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if (is_dir($path . $file)) {
if ((substr($file, 0, 1) != ".") && ($file != "thumb")) {
?>
<input type="text" name="gal_is" value="<?php echo $file; ?>" /><br />
<?php
}
}
}
closedir($handle);
}
?>
<input type="submit" value="Set Gallery" />
</form>
<?php
}
}
if (isset($gal_set)) {
?>
You are uploading into the gallery: <strong><?php echo $gal_set;?></strong>.<br /> If this is not correct please <a href="?clear=<?php echo $gal_set;?>">click here</a>.
<?php
}
?>
</body>
</html>

Nile
02-11-2010, 04:33 AM
I don't understand... do you want to get rid of a variable ($_COOKIE['blah']) or delete the file path that the variable is holding?

djr33
02-11-2010, 04:43 AM
If you are trying to use the cookie to set the directory, there are a few things to consider:
1. Absolutely do NOT use the value directly. In fact, a session variable is probably much better, because the user cannot insert unexpected values and crash your server's file structure.
2. Echo the value and see if the relative path is right. You may need to add a slash, etc.
3. The cookie value will only be available on the NEXT page load, so you can both use setcookie() and $_COOKIE[] =, and that way have it available during that page load. I'm not sure if this is relevant, but when working with cookies for the first time it can get messy. (Again, I'm not sure if this is the case here.)

If you explain a bit more about the overall goal, there may be a better way to approach it. Without knowing more, I suggest using sessions.

bluewalrus
02-11-2010, 05:18 AM
I took out the login section because that works fine but the user has to be logged in to get to this section.

I want the the cookie named gallery to hold the name of the gallery the user was last uploading into. If they want to they can continue to upload into that gallery and display that that's where they are uploading. If not there is a link that delete's the gallery cookie and asks them to select another gallery (listing of directories), or make a new directory.

Let me know if I need to explain any further. Thanks.

djr33
02-11-2010, 05:55 AM
That still means that any logged in user can access ANY file on your server. That's a security risk, user accounts or not. But perhaps you're planning on adding directory-verification anyway.

Regardless, I think that a session is better, unless you mean "last uploading into" in the sense of last visit. Here you could store it in the database (since you're using logins anyway).