JavaScript Cookies VS PHP Cookies

Javascript cookies vs php cookies

HTTP Cookies are not a feature of PHP, nor a feature of Javascript : those are just programming languages that allow a developper to manipulate them.

The biggest difference between JS and PHP is that :
  • Javascript runs on the client side
  • PHP runs on the server side

But cookies are still the same : they are defined as a standard -- see RFC 2965.

Still, note that modern browsers implement cookies that are not accessible from Javascript *(see the `httponly` option of [`setcookie`][3])* -- which means that, depending on the browser, and the way a cookie was set, it might not be accessible from Javascript.

This is a security measure -- and is not a difference between "js cookies" and "php cookies" : it's just a property of some cookies.

Cookies - PHP vs Javascript

They are the same ones, in both cases the cookie is sent to the browser, stored there and the browser send it back to you every request until it expires or is deleted.

For that reason, you should never use cookie for security as your question implies nor for any data which you consider important to keep unaltered by the end user.

There are five things to always remember when you use cookie:

1 - you can not trust its content

2 - you can not assume it will still be there on the next request

3 - you can not trust its content

4 - you can not assume the user never visited before if it's not there

5 - you can not trust its content

If you get that, accessing cookie from php or javascript is simply a question of what's more convenient to you.

Are PHP cookies and 'JS' cookies the same?

A cookie is a cookie, unless it is specified to be HTTP only.

setting cookie through JavaScript vs setting Cookie through PHP

Cookie should still be sent with every request, even if set by javascript.

Only real reason I can think of to set a cookie by javascript is if your saving something modified client side - like the custom look and feel you mentioned.

PHP Sessions vs. Cookies

php sessions, cookies, or javascript cookies?

There is either a session or cookie so there are two things not three.

Now a session is also a cookie but is saved on server unlike simple JS cookie which is saved in user's machine.

I would like to save that data so it repopulates if I leave the page

If it is sensitive information, always use database to store it and if it is not sensitive data:

  • Use cookies or localStorage but it can be deleted by user
  • Use session which can't be deleted by a user but will expire based on php.ini settings

On the other hand, to save it permanently, use the database instead.

how to read php cookies with javascript correctly

<?php
$cookie_name = "test";
$cookie_value = "123";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

You need to write your php code like this

You can use this script If u can want particular cookie

<script>
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}

console.log(getCookie('test'));
<script>


Related Topics



Leave a reply



Submit