An HTTP Cookie or Web Cookie is a small piece of data that is send from the web server to the client browser that can be stored in the client machine for a future look ups.The cookies were primarily designed as a mechanism to maintain a session state of the user sessions without too much network and/or server traffic.There are several good uses of cookies, although it has got a lot of bad press because of misuses by websites and concerns about user privacy. HTTP Cookies can be used to maintain any information as long as it is text, such that the user shopping cart, authentication details etc.If you are developing a website, then it is quite possible that you will need to set cookies on the user client. Of course, the exact requirements of the website will dictate whether you need a cookie at all, but let’s assume that you want to learn how to set and unset cookies from webpages.No matter what framework you use, you can set cookies using simple JavaScript on your page. This is independent of the JS library you might be using such as Dojo or JQuery. Having said that, there are several libraries available that are supposed to ease the pain of managing the cookies.A cookie can contain associated meta information along with the value of the cookie. There are some required values, while many are not required and defaults to reasonable or appropriate value when setting the cookie. The general syntax (or spec) of an HTTP cookie is as shown below.A cookie is set by using the HTTP request header Set-Cookie. The value of this header is the entire cookie that will be parsed and saved in the client browser.Set-Cookie: name=value;Comment=comment;Domain=domain;Max-Age:time-in-seconds;Expires=expiry-time;Path=path;Secure;Version=1Name: This is the name of the cookie. This can be any string that uniquely identifies the cookie. You will need this later if you want to read the value.

Đang xem: How to destroy or remove a cookie in php

Value: This is the value of the cookie. This can at-most be around 4kb. The 4093 byte limit is for the entire cookie, but assuming a reasonable size for other fields, we can specify that the value can have at-most of about 4000 bytes.Comment: An optional field that is rarely used. You specify a string that explains why and what the cookie is used for.Expires: The expires field sets the exact time when the cookie is set to expire or become invalid. The time is set using the format Wdy, DD Mon YYYY HH:MM:SS GMTMax-Age: Another way to specify the expiration time. It specifies the time interval in seconds, which is calculated from the time the cookie is set. If both Max-Age and Expires is set, then Max-Age takes preference.

Xem thêm: Category: Code Viblo – Lập Trình Hướng Đối Tượng Và Kiến Trúc Mvc

Path: You can optionally set the path of the webpage that the cookie is set for.Domain: You can also explicitly set the domain name for which this cookie is set for. If not specified, then the defaults to the domain that requested the URL.Secure: No value need to be specified here. Adding the keyword Secure to the cookie will make sure that only secure and encrypted connections such as HTTPS is used to set the cookie.

Xem thêm: Cách Sửa Lỗi Laptop Không Hiện Wifi, Cách Sửa Lỗi Laptop Không Kết Nối Được Wifi

HttpOnly: The cookie can set and accessed by using only http connections. Other methods such as Javascript calls will not be able to access the cookie.A complete example of a cookie string will be something like:Set-Cookie:mycookiename=”this cookie value”;;Max-Age=3600;Path=/;Secure


You can set cookies using a JavaScript function. A very simple function to set cookie will look something like this:You can now call the function using the various arguments from anywhere in the website. A valid call to set a cookie that is expires in an hour will be likesetCookie(“CookieName”, “Cookie value is not important”, 3600);On a side note, setting the max-age to a negative value will make it to be somewhat of a session cookie, with the browser deleting the cookie when the browser session ends. In order to delete or unset the cookie, call method with the key (or name) of the cookie and 0 (zero) as the age. Setting the max-age to zero instead of a negative value will remove the cookie.setCookie(“CookieName”, “”, 0);


If you are using PHP as your server-side scripting language, then you can set cookies using the setcookie(…) function. The function has the following syntax.bool setcookie(name, value, expires, path, domain, secure, httponly)A quick example issetcookie(“CookieName”, “this cookie value”, time()+3600, “/”, “”);Note that the setcookie(…) method takes the expires as the argument, and not max-age. You will need to set the time at which you want the cookie to expire rather than the time interval as shown in the JavaScript example.As with the JavaScript, you can unset or delete the cookie by setting the expires field to a time and date in the past. A value of 1 for the expires field will do the trick.


You can use the standard JavaScript functions as shown above to set cookies. There is also plugins available in JQuery that allow you to set and unset cookies using JQuery like syntax. One of the popular plugin is jquery-cookie.First include the plugin in your webpage…Ezoicreport this ad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *