Alex Rivera | Logout

Read a PHP cookie from JavaScript

Asked 2011-11-03T20:50:03.540
11

I have a PHP script which sets a cookie called user. I need to be able to read the value from this cookie in javascript.

Can this even be done.

Thanks for any help you can provide.

Edit
Report

3 Answers

16

There's no such thing as a "PHP cookie" or a "javascript cookie". There's just cookies, which you can access from PHP and Javascript. In JS, you'd use document.cookie to access the raw cookie data. There's plenty of libraries which give you finer-grained access as well,

answered 2011-11-03T20:52:52.590
5

You can access your cookies with document.cookie Check this link: http://www.w3schools.com/js/js_cookies.asp

answered 2011-11-03T20:52:38.673
-8

Since PHP is a server side script and JavaScript runs in the browser, you'll need to send the cookie to the browser in a hidden form variable, or use an AJAX call from JavaScript to get it from the server.

answered 2011-11-03T20:51:59.860

Your Answer