Web Hosting Talk







View Full Version : setcookie question


Northern
02-13-2006, 10:49 PM
Hi.
At some point of my site, I need to update a cookie for a user but since updating a cookie is not an option, and what we really do is re declare it rather than updating, I need to keep the other values of it.
For example, some users will go with a cookie that's set to expire after closing the browser and some, in a few days.
So, basically my question is: how do I read other values of a cookie such as the expiry date...etc?

Thanks

PlanetWebHost
02-14-2006, 06:12 AM
what if you were to just set another cookie that stores their expiry time

Burhan
02-14-2006, 06:59 AM
You can read the expiry date of a cookie by checking the response headers. Here is an example:


<?php

setcookie("hello","bar",time()+3600);
echo '<pre>'; print_r($_COOKIE); echo '</pre>';
echo '<pre>'; print_r(getallheaders()); echo '</pre>';
echo '<pre>'; print_r(apache_response_headers()); echo '</pre>';
?>
<a href="#">reload</a>


Response:

Array
(
[hello] => bar
)

Array
(
[Host] => phoenix
[User-Agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
[Accept] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
[Accept-Language] => en-us,en;q=0.5
[Accept-Encoding] => gzip,deflate
[Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
[Keep-Alive] => 300
[Connection] => keep-alive
[Cookie] => hello=bar
[Cache-Control] => max-age=0
)

Array
(
[X-Powered-By] => PHP/4.4.0-pl1-gentoo
[Set-Cookie] => hello=bar; expires=Tue, 14 Feb 2006 11:56:04 GMT
)

reload