Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookies - PHP vs Javascript

Tags:

With regards to security and convenience which cookies are better the PHP ones or the Javascript ones?

like image 982
mkamthan Avatar asked Aug 15 '09 12:08

mkamthan


People also ask

Are PHP and JavaScript cookies same?

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.

Can we use cookies in PHP?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Can JavaScript make cookies?

JavaScript can create, read, and delete cookies with the document. cookie property.


1 Answers

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.

like image 156
Lepidosteus Avatar answered Oct 01 '22 02:10

Lepidosteus