Session Handling Using Cookies

2. document.cookie


          This is the JavaScript document property in creating, accessing and deleting cookies. It contains a string that records cookies in the form of a name=value pair. Inside the string, an expiration date and time of the cookies could be set by using the keyword expires and also where in your computer these cookies will be saved by using another keyword path. The complete syntax in creating a cookie is shown below:

          document.cookie = "username=Mrs. Dee; expires=Wed, 5 May 2021 12:00:00 UTC; path=/";

          wherein username is the name of the cookie with Mrs. Dee as its value and saved inside the root directory of the current drive as indicated by path=/.

          Cookies are removed from a users’ device based on the date and time given in the expires keyword.  In our example above the cookie username would be deleted by May 5, 2021 at 12:00 (+8hours Philippine time). Re-setting the expires keyword past the initial expiration date and time will delete a cookie. If no expiry information is given, cookies are deleted once the browser is closed.

          The syntax for reading and accessing all cookies is shown below:

          var savedCookies = document.cookie;

          The statement above will return all saved cookies in one string in the form of name1=value1; name2=value2;...... plus other information inside the variable savedCookies.