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
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.
Link to comment
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?
Link to comment
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]
Link to comment
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?
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.