Reputation:
Can I edit a cookie created by JavaScript with PHP and vice versa?
Upvotes: 0
Views: 315
Reputation: 3073
Cookies are accessible only by the same origin. Some cookies have rules set, like only accessible by https
or only accessible by *.images.google.com
. It doesn't 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.
Upvotes: 0
Reputation: 2098
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 an 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.
Upvotes: 2
Reputation: 29975
Only web browser stores your cookies. It sends them to script on each request. Script sends them back with a reply.
Upvotes: -1
Reputation: 62884
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'];
?>
Upvotes: 4