How can I delete a cookie rather than the value of the cookie?
I tried this
But that's bringing upPHP Code:unlink($_COOKIE["Gallery"]);
unlink([COOKIE_VALUE]) [function.unlink]: No such file or directory
How can I delete a cookie rather than the value of the cookie?
I tried this
But that's bringing upPHP Code:unlink($_COOKIE["Gallery"]);
unlink([COOKIE_VALUE]) [function.unlink]: No such file or directory
Corrections to my coding/thoughts welcome.
bluewalrus (02-11-2010)
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
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
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 Code:<?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>
Corrections to my coding/thoughts welcome.
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?
Jeremy | jfein.net
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.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
bluewalrus (02-11-2010)
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.
Corrections to my coding/thoughts welcome.
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).
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks