Jump to content

Why cant php kill this cookie?


Prismatic

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
Link to comment
https://forums.phpfreaks.com/topic/15195-why-cant-php-kill-this-cookie/
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.
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?
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]
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?
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]

Archived

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

×
×
  • Create New...

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.