What are the differences between JavaScript and PHP cookies?

WBOY
Release: 2023-09-02 12:30:02
forward
1086 people have browsed it

What are the differences between JavaScript and PHP cookies?

JavaScript Cookie

Using JavaScript cookies is the most effective way to remember and track preferences, purchases, commissions and other information. Information needed for a better visitor experience or website statistics.

PHP Cookies

Cookies are text files that are stored on the client's computer and retained for tracking purposes. PHP transparently supports HTTP cookies.

How do JavaScript cookies work?

Your server sends some data to your visitor's browser in the form of a cookie. Browsers can accept cookies. If present, it will be stored on the visitor's hard drive as a plain text record. Now, when the visitor reaches another page on the site, the browser sends the same cookie to the server for retrieval. Once retrieved, your server will know/remember what was previously stored.

The data contained in the cookie is automatically transferred between the web browser and the web server so that CGI scripts on the server can read and write the cookie value stored on the client.

JavaScript can also use the cookie property of the Document object to manipulate cookies. JavaScript can read, create, modify, and delete cookies applied to the current web page.

How do PHP cookies work?

Cookies are typically set in HTTP headers (although JavaScript can also set cookies directly on the browser). A PHP script that sets a cookie may send a header like the following –

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
Copy after login

As you can see, the Set-Cookie header contains name-value pairs, GMT date, path, and domain. Names and values ​​will be URL encoded. The "expires" field instructs the browser to "forget" the cookie after a given time and date.

PHP provides the setcookie() function to set cookies. This function takes up to six arguments and should be called before the marker.

The above is the detailed content of What are the differences between JavaScript and PHP cookies?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!