views:

44

answers:

3
+3  Q: 

Gathering cookies

I'm not even sure how to describe what I'm trying to do since I don't know a whole lot about cookies, but here goes.

Is it possible to use PHP to collect a cookie (or cookie file) from the browser cache, save it into the database, then clear the cache and re-save it back to where it used to be?

What would be involved in implementing this? Reading from the browser cache? Do I need to give it a path to where the cookies are saved, or can it automatically get it through communicating with the browser directly? Can PHP handle that?

+2  A: 

Get cookie data:

<?php
echo 'Hello ' . htmlspecialchars($_COOKIE["name"]) . '!';
?>

Assuming the "name" cookie has been set earlier The above example will output something similar to:

Hello Hannes!

You could iterate over all cookies in the array with:

foreach($_COOKIES as $name => $cookie){
   echo "Cookie: $name has a delicious center consisting of: ";
   print_r($cookie);
}

http://www.php.net/manual/en/reserved.variables.cookies.php

Set cookies:

http://www.php.net/manual/en/function.setcookie.php

Michael Robinson
+4  A: 

In PHP you can read all cookies from the user's system (that you are authorised to read) from the $_COOKIE array.

You can clear these cookies on the user's system by using setcookie() and setting them to a blank string, though you often need to know the exact hostname and path the cookies were previously set under, and give the same ones again, to be able to delete them properly.

You can then set the cookies again later to the values you previously got from the $_COOKIE array.

Not all in the same page view of course.

thomasrutter
+2  A: 

PHP has nothing to do with client's computer in general and browser cache in particular.
PHP can get only the cookie, which browser send by it's free will and which were set by same server before.

Of course a server may send an empty or expired cookie which is equal to delete, or the same cookie with another value

Col. Shrapnel