Usually, the website designer uses the backstage database to achieve the above goal. When the user first accesses the Web site, the site creates a new ID in the database and sends the ID to the user via a cookie. When the user visits again, the website adds 1 to the counter of the user ID, gets the user's visit number or determines whether the user is a new user or an old user.
The server can use cookies to contain information about the arbitrary nature to filter and regularly maintain this information to determine the status in the HTTP transmission. The most typical use of cookies is to determine whether registered users have logged on to the site, users may be prompted, whether the next time to enter this site to retain user information to simplify the login procedures, these are the function of cookies. Another important application is the "shopping cart" process. Users may select different items on different pages of the same Web site for a period of time, which will be written to cookies to extract information at the final payment.
// Create a cookie
function createCookie (userName, password) {
var date = new Date ();
date.setTime (date.getTime () + 60 * 1000 * 60 * 24); // Save the time of day
document.cookie = "userName =" + escape (userName) + "; expires =" + date.toUTCString ();
// escape function encodes String, setTime is the lifetime of the cookie
document.cookie = "password =" + escape (password) + "; expires =" + date.toUTCString ();
alert (document.cookie);
}
// Get cookie data
function getCookie (value) {
var cookieString = document.cookie;
var start = cookieString.indexOf (value + "="); // Find the position of value
if (start ==-1)
return null;
start + = value.length + 1; // length of value + 1, which is the position after =
var end = cookieString.indexOf (";", start); // length of valuer +1 to find;
if (end ==-1) // In other words, there is only one cookie so there is no;
return cookieString.substring (start);
return cookieString.substring (start, end);
}
// Delete cookies
function delCookie () {
var date = new Date ();
var user = getCookie ("userName");
var pwd = getCookie ("password");
date.setTime (date.getTime ()-1);
// Decrease the current time -1 milliseconds with the current system time and become a negative value, then this cookie expires when stored in the system and will be destroyed
document.cookie = "userName =" + escape (user) + ";" + "expires =" + date.toUTCString ();
document.cookie = "password =" + escape (pwd) + ";" + "expires =" + date.toUTCString ();
}
how php gets Cookies