The web servers host the website. The client-server makes a request for data from the webserver and the webserver fetches the required pages and responds to the client by sending the requested pages. The web server communicates with the client-server using HTTP (HyperText Transfer Protocol). HTTP is a stateless protocol which means the server needs not to retain the user information once the transaction ends and the connection is closed. The web browser is an example of a client-server which communicates with the web server using HTTP. HTTP prevents long engagement of the client with the webserver and the connection is closed automatically once the request is serviced. But often it is required to store the user information for future references. One of the most common uses of cookies is for authentication. Cookies serve the purpose of retaining user information even when the connection is lost. Cookies are data, stored in text files, on the computer.
Cookies comprise of five variable fields:
javascript
Read cookie using JavaScript: This function retrieves the cookie data stored in the browser. The cookie string is automatically encoded while sending it from the server to the browser. Hence it needs to be decoded before the actual data can be retrieved. Next, the decoded string is split into an array to get all the cookie name-value pairs. Loop through the array to find the field-name and respective field-values. If the cookie is found, the value is returned else the function returns the empty string.
Program:
javascript
Create and read cookies using JavaScript: When the webpage is loaded the runApp() function is called and it checks if there exists a cookie in the browser it is retrieved else a new cookie is created for the same.
Program:
html
- Expires:Specifies when the cookie will expire. If left empty the cookie expires immediately when the connection is lost.
- Domain: Specifies the domain name of the website.
- Name=Value: Cookies are stored in the form of name-value pairs.
- Path: Specifies the webpage or directory that sets the cookie.
- Secure: Specifies whether the cookie can be retrieved by any server (secure or non-secure).
function createCookie(fieldname, fieldvalue, expiry) {
var date = new Date();
date.setTime(date.getTime()+ (expiry*24*60*60*1000));
var expires = "expires=" + date.toGMTString();
document.cookie = fieldname + "=" + fieldvalue +
";" + expires + ";path=/";
}
function readCookie(cname) {
var name = cname + "=";
var decoded_cookie = decodeURIComponent(document.cookie);
var carr = decoded_cookie.split(';');
for(var i=0; i<carr.length;i++){
var c = carr[i];
while(c.charAt(0)==' '){
c=c.substring(1);
}
if(c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
<!DOCTYPE html>
<html>
<head>
<title>
Create and read cookies
using JavaScript
</title>
<script type="text/javascript">
function createCookie(fieldname, fieldvalue, expiry) {
var date = new Date();
date.setTime(date.getTime()+ (expiry*24*60*60*1000));
var expires = "expires=" + date.toGMTString();
document.cookie = fieldname + "=" + fieldvalue
+ ";" + expires + ";path=/";
}
function readCookie(cname) {
var name = cname + "=";
var decoded_cookie =
decodeURIComponent(document.cookie);
var carr = decoded_cookie.split(';');
for(var i=0; i<carr.length;i++){
var c = carr[i];
while(c.charAt(0)==' '){
c=c.substring(1);
}
if(c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function runApp() {
var user = readCookie("username");
if(user != ""){
alert("Hello "+user);
}else{
user=prompt("Enter your name: ", "");
if(user!= "" && user!=null){
createCookie("username", user, 30);
}
}
}
</script>
</head>
<body onload="runApp()"></body>
</html>
Output:
- Creating Cookie:

- Reading Cookie:
