
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What are the differences between JavaScript and PHP cookies?
JavaScript Cookies
Using JavaScript cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics.
PHP Cookies
Cookies are text files stored on the client computer and they are kept for tracking purpose. PHP transparently supports HTTP cookies.
How JavaScript cookies work?
Your server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie. If it does, it is stored as a plain text record on the visitor's hard drive. Now, when the visitor arrives at another page on your site, the browser sends the same cookie to the server for retrieval. Once retrieved, your server knows/remembers what was stored earlier.
The data contained in a cookie is automatically transmitted between the web browser and the web server, so CGI scripts on the server can read and write cookie values that are stored on the client.
JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookies that apply to the current web page.
How PHP cookies work?
Cookies are usually set in an HTTP header (although JavaScript can also set a cookie directly on a browser). A PHP script that sets a cookie might send headers that look something like this –
HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name = xyz; expires = Friday, 04-Feb-07 22:03:38 GMT; path = /; domain = tutorialspoint.com Connection: close Content-Type: text/html
As you can see, the Set-Cookie header contains a name-value pair, a GMT date, a path and a domain. The name and value will be URL encoded. The “expires” field is an instruction to the browser to "forget" the cookie after the given time and date.
PHP provideS setcookie() function to set a cookie. This function requires upto six arguments and should be called before <html> tag.
- Related Articles
- What are cookies in JavaScript?
- What are the differences between inline JavaScript and External file?
- What are the differences between mean.io and mean.js in javascript?
- What are the differences between clientHeight() and offsetHeight() in javascript?
- What are the differences between JavaScript Primitive Data Types and Objects?
- what are the differences between unshift() and push() methods in javascript?
- What are the differences between Deferreds, Promises and Futures in javascript?
- What are secured cookies in JavaScript?
- What records are present in JavaScript cookies?
- What are the differences in die() and exit() in PHP?
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between holography and photography?
- What are the differences between lodash and underscore?
- What are the differences between solvency and liquidity?
