Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unset Cookie Button

    Would anyone have an idea on how to create this?

    My site creates a cookie based on a users choice, but I want to include some sort of reset/unset/delete cookie button that removes this data and gives them a fresh start, probably refreshing the page.

    I have a very basic knowledge of php so any support is greatly appreciated.

    Thank you

  • #2
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Code:
    if(isset($_POST['submit']))
    {
        setcookie(cookiename, gone, time() - 100);
    }

  • Users who have thanked melloorr for this post:

    Jallen57 (01-03-2012)

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you, how would I implement this?

  • #4
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by Jallen57 View Post
    Thank you, how would I implement this?
    Have a form on a page:
    Code:
    <?php
    if(isset($_POST['submit']))
    {
        setcookie(cookiename, gone, time() - 100);
    }
    ?>
    
    <html>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
    <input type="submit" name="submit" value="Delete Cookie">
    </form>
    </html>

  • Users who have thanked melloorr for this post:

    Jallen57 (01-03-2012)

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm having an issue with this part:

    Code:
    <?php echo $_SERVER['PHP_SELF']?>
    What do I need to change here?

  • #6
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by Jallen57 View Post
    I'm having an issue with this part:

    Code:
    <?php echo $_SERVER['PHP_SELF']?>
    What do I need to change here?
    Well that just sends that data to the same page, so if you want, you can just type in the name of the file. e.g. action="deletecookie.php"

  • Users who have thanked melloorr for this post:

    Jallen57 (01-03-2012)

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It works but only once you refresh the page manually after clicking the button.

    Any way around this? Thank you for your help so far!

  • #8
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by Jallen57 View Post
    It works but only once you refresh the page manually after clicking the button.

    Any way around this? Thank you for your help so far!
    No, don't forget, once you click the submit button, the script is run, THEN the cookie is deleted.
    actually, in the php script, you could have:
    Code:
    header("Location: index.php");
    under everything, so once the cookie is deleted, they are sent to the index page
    Last edited by melloorr; 01-03-2012 at 01:02 PM.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by melloorr View Post
    Code:
    if(isset($_POST['submit']))
    {
        setcookie(cookiename, gone, time() - 100);
    }
    Don't rely on this button always being submitted. IE has a bug which has still not been fixed by MS since V5 (No idea about V4 and below). I'd recommend checking for a hidden field instead.

    See my signature for details.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It's not deleting the cookie when I use the method action="deletecookie.php"

    Where it was working is when I put

    Code:
    <?php
    if(isset($_POST['submit']))
    {
        setcookie(PHPSESSID, gone, time() - 100);
    }
    ?>
    In my header.php file, then had action="index.php"

    But it would only work after I refreshed the page.

    Is it something to do with wordpress? I really can't understand why it isn't deleting it.

  • #11
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Is there some sort of refresh button I can make that also resets cookies for the user instead?

  • #12
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    I put deletecookie.php just to show how to put your file name.

    And in your delete cookie script it should be:
    Code:
    setcookie(PHPSESSID, gone, time() - 100);
    header("Location: index.php")

  • #13
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes I've done that but it doesn't reset the cookie

  • #14
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Replace PHPSESSID with the name of the cookie you want to delete. So, the form should be:
    PHP Code:
    <form action="deletecookie.php" method="post">
    <
    input type="submit" value="Delete cookie" />
    </
    form
    And deletecookie.php should be:
    PHP Code:
    setcookie('name_of_cookie','dummy value',time()-100);
    header("location: index.php"); 
    Done.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #15
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    You are not listening to tangoforce
    Use this

    Code:
    <?php
    if (isset($_POST['Msoft']))
    {
    	if($_POST['Msoft'] == 'garbage')
    	{
    	    setcookie(cookiename, '', time()-3600, '/','',0);
    	}
    }
    
    ?>
    
    <html>
    <form action='' method="POST">
    <input type="submit" name="submit" value="Delete Cookie">
    <input type="hidden" name="Msoft" value="garbage">
    </form>
    </html>
    and don't forget to put the REAL cookie name in there!
    Last edited by sunfighter; 01-03-2012 at 04:36 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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