Jump to content


Photo

Why cant php kill this cookie?


  • Please log in to reply
9 replies to this topic

#1 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 20 July 2006 - 08:36 PM

It gets set using some javascript (this works fine)
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=/"; 
}

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

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

But that doesn't work either :(

How can I kill this darn thing? :P

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 July 2006 - 08:40 PM

From groups.google.com:

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.


...or you can get some milk; always works for me.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 20 July 2006 - 09:12 PM

Well the cookie is created when a link is clicked
<a onclick='savedata()' href='post.php?id=". $getpostid ."&brd=". $TopicBoardID ."&act=reply'>Go Advanced</a>

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?

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 July 2006 - 09:18 PM

This example works for me:

<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>';
	}
?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 20 July 2006 - 09:19 PM

I can view the cookies contents just fine. I just cant seem to be able to get rid of it.

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 July 2006 - 09:24 PM

what's the host setting of the cookie? sometimes if the host and path don't match, it won't get deleted.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 July 2006 - 09:29 PM

Manual:

Cookies must be deleted with the same parameters as they were set with.


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#8 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 20 July 2006 - 09:31 PM

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,
$time = time();
$expiretime = $_POST['expiry'];
$expiretime = $time + $expiretime ;
setcookie ("BoardCookieV3",$CookieContents, $expiretime, "/", $CookieDomain

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

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

#9 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 July 2006 - 09:44 PM

This is interesting--perhaps the manual only applies to PHP itself. Anyhow, this works for me:

<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, '/');
		}
	}
?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#10 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 20 July 2006 - 10:04 PM

Yep, that appears to work aswell :) Thanks for the help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users