Jallen57 01-03-2012, 11:53 AM 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
melloorr 01-03-2012, 12:18 PM if(isset($_POST['submit']))
{
setcookie(cookiename, gone, time() - 100);
}
Jallen57 01-03-2012, 12:23 PM Thank you, how would I implement this?
melloorr 01-03-2012, 12:26 PM Thank you, how would I implement this?
Have a form on a page:
<?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>
Jallen57 01-03-2012, 12:49 PM I'm having an issue with this part:
<?php echo $_SERVER['PHP_SELF']?>
What do I need to change here?
melloorr 01-03-2012, 12:52 PM I'm having an issue with this part:
<?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"
Jallen57 01-03-2012, 12:55 PM 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!
melloorr 01-03-2012, 12:58 PM 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:
header("Location: index.php");
under everything, so once the cookie is deleted, they are sent to the index page
tangoforce 01-03-2012, 01:17 PM 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.
Jallen57 01-03-2012, 01:19 PM It's not deleting the cookie when I use the method action="deletecookie.php"
Where it was working is when I put
<?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.
Jallen57 01-03-2012, 01:21 PM Is there some sort of refresh button I can make that also resets cookies for the user instead?
melloorr 01-03-2012, 01:47 PM I put deletecookie.php just to show how to put your file name.
And in your delete cookie script it should be:
setcookie(PHPSESSID, gone, time() - 100);
header("Location: index.php")
Jallen57 01-03-2012, 02:04 PM Yes I've done that but it doesn't reset the cookie :(
BluePanther 01-03-2012, 04:30 PM Replace PHPSESSID with the name of the cookie you want to delete. So, the form should be:
<form action="deletecookie.php" method="post">
<input type="submit" value="Delete cookie" />
</form>
And deletecookie.php should be:
setcookie('name_of_cookie','dummy value',time()-100);
header("location: index.php");
Done.
sunfighter 01-03-2012, 04:33 PM You are not listening to tangoforce
Use this
<?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!
BluePanther 01-03-2012, 05:01 PM You are not listening to tangoforce
If you actually read my example, you'll see it doesn't need to check for the post information. There's no need to, as deletecookie.php will have no other task.
Your example contains invalid markup (no head information, or body tags). It will, also, not fulfil the OP's needs. His functionality requires his script to 'refresh' the page he was on AFTER the cookie will be deleted. Yours will not do this.
Note, as well, I have a very similar signature to tangoforce :thumbsup:
tangoforce 01-03-2012, 06:29 PM Note, as well, I have a very similar signature to tangoforce who I bow down to and copy for being such a superior coder to me :thumbsup:
Oh BP, you're far too kind :D
Jallen57 01-03-2012, 11:50 PM Hi guys, it still didn't work however I worked it out in the end, I used this:
session_start(); // initialize session
session_destroy(); // destroy session
setcookie("PHPSESSID","",time()-3600,"/"); // delete session cookie
header("location: mywebsite.com");
Thanks for your help all the same!
|
|