sehummel
sehummel

Reputation: 5568

PHP and Javascript cookies

Can I access a cookie written with jQuery's cookie plug-in with PHP? I know you can't set Javascript equal to PHP or vice versa, but IN ESSENCE is:

$.cookie('var') = $_COOKIE['var']?

Again, I know you can't set them equal to each other, but if I set it in jQuery and then go to another page, can PHP access it? I've read lots of posts about this, but I can't seem to find an answer to this part.

Note, if I look in Firefox's preferences, I can see the cookies are there, so I know they're set.

Upvotes: 1

Views: 546

Answers (2)

josh3736
josh3736

Reputation: 144912

Yes.

But... Klaus Hartl's jQuery cookie plugin (I'm assuming this is the one you are using) defaults to setting the cookie's path to that of the current document. If the PHP file you were trying to echo from was in a different folder, the cookie wouldn't have been sent.

You can set the cookie's path when you set the cookie:

$.cookie('name', val, { path: '/' });

Upvotes: 1

Valera Leontyev
Valera Leontyev

Reputation: 1181

The cookies you have in PHP is the same that you have in JavaScript. They are equal. Cookies have some options to set acceess scope, lifetime and so on. If you'll set a cookie for all the domain, it will be seen in PHP.

Upvotes: 0

Related Questions