Jump to content

Archived

This topic is now archived and is closed to further replies.

Prismatic

Why cant php kill this cookie?

Recommended Posts

It gets set using some javascript (this works fine)
[code]function setCookie(name, value){
var d = document;
var today = new Date();
var expiry = new Date(today.getTime() + 30 * 24 * 60);
d.cookie = name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/";
}[/code]

And I want to use php to kill it right away.

ive tried this
[code]setcookie("MyCookie", "", time()-999999);[/code]
But that doesn't kill it, so I figured I could try and make it an empty cookie:
[code]setcookie("MyCookie", "");[/code]

But that doesn't work either :(

How can I kill this darn thing? :P

Share this post


Link to post
Share on other sites
From groups.google.com:

[quote]
Be aware that when testing cookies this way, they will be available before
the cookie header is send. This is because in JavaScript, 'document' is an
object and 'cookie' a property of this object.

In PHP, the cookie is available not sooner then after a reload of the page.
[/quote]

...or you can get some milk; always works for me.

Share this post


Link to post
Share on other sites
Well the cookie is created when a link is clicked
[code]
<a onclick='savedata()' href='post.php?id=". $getpostid ."&brd=". $TopicBoardID ."&act=reply'>Go Advanced</a>[/code]

So it's set on that page, and then it follows the href to the other page. So technically, doesn't it get reloaded? Shouldn't I have access to it?

Share this post


Link to post
Share on other sites
This example works for me:

[code]
<script type="text/javascript" language="javascript">
function setCookie(name, value){
var d = document;
var today = new Date();
var expiry = new Date(today.getTime() + 30 * 24 * 60);
d.cookie = name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/";
}
setCookie('test','ing');
</script>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="submit" type="submit" />
</form>

<?php
if ($_POST) {
echo 'PHP Cookies<br /><pre>', print_r($_COOKIE, true), '</pre>';
}
?>
[/code]

Share this post


Link to post
Share on other sites
I can view the cookies contents just fine. I just cant seem to be able to get rid of it.

Share this post


Link to post
Share on other sites
what's the host setting of the cookie? sometimes if the host and path don't match, it won't get deleted.

Share this post


Link to post
Share on other sites
Manual:

[quote]Cookies must be deleted with the same parameters as they were set with.[/quote]

Share this post


Link to post
Share on other sites
I dunno, it gets set as you see it there on the javascript. This is how I set my login cookie when a user logs in,
[code]
$time = time();
$expiretime = $_POST['expiry'];
$expiretime = $time + $expiretime ;
setcookie ("BoardCookieV3",$CookieContents, $expiretime, "/", $CookieDomain
[/code]

expiry is a list, it's all in seconds.

How would I modify the javascript setcookie() to set it the same?

Share this post


Link to post
Share on other sites
This is interesting--perhaps the manual only applies to PHP itself. Anyhow, this works for me:

[code]
<script type="text/javascript" language="javascript">
function setCookie(name, value){
var d = document;
var today = new Date();
var expiry = new Date(today.getTime() + 30 * 24 * 60);
d.cookie = name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/";
}
</script>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="cookier" type="button" value="Set Cookie" onclick="javascript:setCookie('test','ing')" />
<input name="submit" type="submit" value="Refresh" />
</form>

<?php
if ($_POST) {
echo 'PHP Cookies<br /><pre>', print_r($_COOKIE, true), '</pre>';
if ($_COOKIE) {
echo 'Killing cookie...';
setcookie('test', 'ing', 1, '/');
}
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.