×


What are cookies and how to set and get them with Javascript

Cookies are data stored as text files in the computer.They are used store data that are needed to be remembered , like user information.Cookies can be set in client side and server side.

For example,consider a user sign in to your website.The server sends back some response.The response have a token/id that can be stored as a cookie.This cookie value can keep the users details status in the client side.

Cookie has a name and value pair,expiry time,path and secure . The security flags that are used to provide more security

HttpOnly – HttpOnly flag sets the cookie unavailable to client side scripts.This prevent cross site scripting attacks(XSS)

secure – Sets cookie to be send via a secure encrypted channel.Browser will send cookie only via an HTTPS request and prevent sending cookie via HTTP request.

setcookievalue

This function has three arguments

name – cookie name.

value – value of the cookie.

hours- hours determine the expiry hours.

document.cookie lets us store the cookie associated with the current document.

getCookie

This function has one argument ,cookie name

document.cookie gets all the cookies associated with the current document.A cookie with a specific name can be obtained using the getCookie function.

 

getCookievalue

This function has one argument,cookie name

This function checks if a cookie with a particular name is set or not.

deleteCookie

This function has one argument,cookie name .It unsets the cookie with the given name.

Updating cookie – Overwriting the cookie will rewrite the cookie. Calling setcookieValue with the new data will update the cookie value.

SOURCES

http://php.net/manual/en/features.cookies.php

https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

 

 

No Comments

Leave a Reply

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