An HTTP cookie (also known as web cookie, browser cookie) is a small piece of information stored by the server in the user's browser. Cookies are commonly used for session management, user-tracking, and storing user preferences.
Creating a Cookie
Since the cookie value can not contain semicolons, commas, or spaces, you need the
Cookie Expiration Date
By default, the above cookie lifespan is the current browser session, which means that it is removed when the user closes the browser. Such cookies are called session cookies.
To persist cookies beyond the current browser session, you need to specify its expiry date either using the
By default, a cookie is available to all web pages in the same directory and its subdirectories. However, you can explicitly specify a
For example, if set the
The cookies, by default, are available only to the web pages in the domain they were used to set in. However, you can use the
For exmaple, if a cookie created by a web page on
You can also make a cookie secure using the
Now to set a cookie that lasts 3 months, use the following code:
Reading a Cookie
Since all the values and names are URL-encoded, you have to decode them using the
Let us write a function that takes the cookie name as input and returns its value. If the cookie is not found, it should return a
Updating a Cookie
You can update a cookie in the same way as you create it with the same name, path, domain, and secure option:
Alternatively, you could also use the above
Deleting a Cookie
Deleting a cookie is very simple. All you need to do is set the expiration date to some time in the past with the same name, path, domain, and secure option:
You can also use the
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.
How do I put a secure flag on a cookie?
Launch Google Chrome and go to either WEB or CAWEB portal website. Press F12 (from Keyboard) to launch Developer Tools. Go to Application tab -> Cookies ( left Panel) and ensure the Secure column was ticked.
What is secure flag in cookie?
The Secure flag is used to declare that the cookie may only be transmitted using a secure connection (SSL/HTTPS). If this cookie is set, the browser will never send the cookie if the connection is HTTP. This flag prevents cookie theft via man-in-the-middle attacks.
How do you make cookies secure?
You can ensure that cookies are sent securely and aren't accessed by unintended parties or scripts in one of two ways: with the Secure attribute and the HttpOnly attribute. A cookie with the Secure attribute is only sent to the server with an encrypted request over the HTTPS protocol.