Results 1 to 8 of 8

Thread: Delete Cookie

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Delete Cookie

    How can I delete a cookie rather than the value of the cookie?

    I tried this

    PHP Code:
    unlink($_COOKIE["Gallery"]); 
    But that's bringing up

    unlink([COOKIE_VALUE]) [function.unlink]: No such file or directory
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try unset(). Unlink will get rid of the file path with that name.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    bluewalrus (02-11-2010)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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

  5. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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_name0777);
                
    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($file01) != ".") && ($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.

  6. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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

  8. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (02-11-2010)

  9. #7
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.

  10. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •