Jump to content
dalawh

PHP cookies vs JavaScript cookies

Recommended Posts

The ways to read them are different, but the contents of the cookies are the same.Things go like this:1. Open up a page for the first time.2. PHP may set cookies.3. Afterwards, JavaScript may set additional cookies (I think JavaScript has access to cookies set by PHP, but I'm not sure honestly.4. Open up another page from the same domain (or refresh the same page).5. PHP receives cookies previously set by PHP or JavaScript, and/or may set additional cookies (Note: Additional cookies become available on the next request).6. Afterwards, JavaScript has access to the previously set cookies by PHP or JavaScript, and may set additional cookies (again, I think JavaScript has access to the cookies that were set by PHP at step 5, but I'm not sure).

Share this post


Link to post
Share on other sites
The ways to read them are different, but the contents of the cookies are the same. Things go like this:1. Open up a page for the first time.2. PHP may set cookies.3. Afterwards, JavaScript may set additional cookies (I think JavaScript has access to cookies set by PHP, but I'm not sure honestly. 4. Open up another page from the same domain (or refresh the same page).5. PHP receives cookies previously set by PHP or JavaScript, and/or may set additional cookies (Note: Additional cookies become available on the next request).6. Afterwards, JavaScript has access to the previously set cookies by PHP or JavaScript, and may set additional cookies (again, I think JavaScript has access to the cookies that were set by PHP at step 5, but I'm not sure).
Will the PHP and JavaScript save to the same file for the cookie or different ones? The reason I ask this is when I read the JavaScript tutorial on cookies, it seems you define your own cookie and search method, but in the PHP tutorial, everything seems to be predefined. So they will be different.

Share this post


Link to post
Share on other sites

Yeah, that's the difference between PHP and JavaScript cookie handling really... PHP automatically turns the cookie file's contents into an array, and sets cookies in a fashion compatible with that.JavaScript instead just reads the cookie file's contents itself, without turning it into anything else, leaving you to process that file yourself in whatever fashion you may want, not necessarily compatible with what PHP is doing.The "turning it into something else" (the term in this case being "parsing") is different, but the raw content is the same.

Share this post


Link to post
Share on other sites
Guest So Called

As far as I know cookies are just cookies, browser cookies. Browsers handle cookies the same no matter whether set or read by PHP or JavaScript. The languages differ in how they process cookies. There's no reason why you couldn't set a cookie in one of the two and then read it in the other. As far as that goes, you can set cookies in other languages, and even set/test them via Apache .htaccess files.

Share this post


Link to post
Share on other sites

In JavaScript, http://www.w3schools.com/js/js_cookies.asp, you can manually define your own cookies when you create them, but in PHP, http://www.w3schools.com/php/php_cookies.asp, they are predefined. JS allows you to choose your content, but PHP only allows the cookie to have the predefined parameters. Since you said it is all the same, does that mean a cookie can have anything in it just like the JS version, but the PHP version keeps it simple? When you create and search a cookie using PHP, is the cookie page/file similar to the JavaScript, where it is varName = varValue; varName2 = varValue2; etc?

Share this post


Link to post
Share on other sites

Cookies aren't predefined in PHP. Just like in Javascript, they have a name, a value and an expiration date. Additionally you can choose whether you want the cookie available to other subdomains or not. The value of a cookie can be anything, there are no restrictions. The cookie file on the client's computer is exactly the same one for both Javascript and PHP.

Share this post


Link to post
Share on other sites

The more important thing to keep in mind is "one cookie file" vs. "multiple cookies stored within one cookie file". JavaScript lets you read the file, and doesn't give you anything for the cookies themselves, while PHP does the opposite - gives you means to read/write individual cookies within the file, while eliminating access to the file (because why would you want that when you have the individual cookies?).

Since you said it is all the same, does that mean a cookie can have anything in it just like the JS version, but the PHP version keeps it simple?
Essentially... yes.

Share this post


Link to post
Share on other sites
Cookies aren't predefined in PHP. Just like in Javascript, they have a name, a value and an expiration date. Additionally you can choose whether you want the cookie available to other subdomains or not. The value of a cookie can be anything, there are no restrictions. The cookie file on the client's computer is exactly the same one for both Javascript and PHP.
According to PHP tutorial on cookies, the setcookie() method only allows up to 5 parameters, so it is predefined as in, it must have no more than those 5 parameters. In the JavaScript tutorial, I didn't see the mention of parameters, you just kind of defined it to your liking since the the function that creates the cookie is made by each person. So does that mean that cookies can only have up to 5 values, which are name, value, expire, path, and domain? If they can have more than that, what else can they have?

Share this post


Link to post
Share on other sites

The PHP reference on W3Schools is a little outdated (no wonder, PHP develops rapidly). In the latest PHP versions, setcookie() has 7 parameters. See setcookie() in the official PHP manual.For a moment there, I saw what I thought is an error in the W3Schools example, but after seeing Mozilla's docs on document.cookie, it seems the example is correct, although the explanation is somewhat poor - instead of writing to the whole file, you just write one cookie as a string, and the browser automatically merges the cookie's name and value to the cookie file. The string itself follows a format that, in the end, makes setting a cookie similar to setting a cookie with PHP's setcookie().

Share this post


Link to post
Share on other sites

The five (or seven) parameters aren't data to be stored, they just define settings for the cookie. Both Javascript and PHP can only have one single value for each cookie. The value can be as long and complicated as you want it to be. There's nothing that Javascript can do with cookies that PHP can't.

Share this post


Link to post
Share on other sites
The PHP reference on W3Schools is a little outdated (no wonder, PHP develops rapidly). In the latest PHP versions, setcookie() has 7 parameters. See setcookie() in the official PHP manual. For a moment there, I saw what I thought is an error in the W3Schools example, but after seeing Mozilla's docs on document.cookie, it seems the example is correct, although the explanation is somewhat poor - instead of writing to the whole file, you just write one cookie as a string, and the browser automatically merges the cookie's name and value to the cookie file. The string itself follows a format that, in the end, makes setting a cookie similar to setting a cookie with PHP's setcookie().
Oh, that makes more sense. Thanks for explaining this.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...