tags:

views:

80

answers:

4

Can I edit a cookie created by javascript with php and vice versa

is a cookie a cookie basically?

+3  A: 

Yes, a cookie is a cookie.

setcookie.html:

<script type="text/javascript">
document.cookie = 'foo=bar';
</script>
<a href="readcookie.php">Did it work?</a>

readcookie.php:

<?PHP 
echo 'This should say "bar": ' . $_COOKIE['foo'];
?>
timdev
makes sensethanks dude
mjr
A: 

Cookies are accessible only by same origin. Some cookies have rules set, like "only accessible by https" or "only accessible by *.images.google.com". It doesn't not matter if the cookie is set via JS or Php so long as it is saved by the browser using the same origin parameters.

Access (read OR write) of the cookie is totally up to the browser, though the behavior is specified by RFC 2109.

thesmart
+2  A: 

Yes you can. However, be aware that the cookie must allow for javascript to edit it. There is a flag (HttpOnly) that can be added to a HTTP cookie header which disallows editing of cookies by browser scripting languages like Javascript in supported browsers. You can see it in the function signature:

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

If it is set to true (the default is false) then the cookie cannot be edited using Javascript.

stinkypyper
A: 

Only web browser stores your cookies. It sends them to script on each request. Script sends them back with a reply.

FractalizeR