Settings of session, cookie, session Storage, local Storage and get deletion

Posted by macleo on Sat, 08 Jun 2019 21:40:11 +0200

I. cookie

What is Cookie?

"Cookie is a variable stored in the visitor's computer. This cookie is sent whenever the same computer requests a page through a browser. You can use JavaScript to create and retrieve cookie values. - w3school

Cookies are files created by visited websites to store browsing information, such as personal data.   

From a JavaScript perspective, cookie s are string information. This information is stored in the client computer and used to transfer information between the client computer and the server.   

In JavaScript, you can read or set this information through document.cookie. Because cookies are mostly used to communicate between client and server, besides JavaScript, server-side languages (such as PHP) can also access cookies.

 

Note: If it's a page on a local disk, the chrome console can't use JavaScript to read and write cookie s. The solution is ____________.

 

1. Setting cookie s

When using JavaScript to access cookies, you must use the cookie attribute of the Document object; a line of code describes how to create and modify a cookie:

If you want to store more than one name/value pair at a time, you can use semicolons with spaces (;)

 

document.cookie="userIdsss=666; userNameee=lucas;path=/; domain="www.google.cn; expires=30; secure";  //Setting up multiple cookie s at the same time

Tip: All attributes in cookies are green

In the above code,'usernameee'denotes the cookie name, and'lucas' denotes the corresponding value of the name. Assuming that the cookie name does not exist, a new cookie is created; if it exists, the corresponding value of the cookie name is modified. If you want to create cookies many times, you can reuse this method.
The name or value of the cookie input cannot contain semicolons (;), commas (,), equal signs (=) and spaces (which need to be coded with escape() functions), and the value is converted back through unescape() functions when the cookie is obtained.

Setting the cookie attribute to secure (belonging to https protocol) only ensures that the data transmission between cookie and server is encrypted, while cookie files stored locally are not encrypted. If you want the local cookie to be encrypted, you have to encrypt the data yourself.


Function Writing:

function setCookie(c_name, value, expiredays) {
  var exdate = new Date();    //Initialization time
  exdate.setDate(exdate.getDate() + expiredays);    //Setting up validity period
  //The path needs to be filled in, because JS The default path is the current page, if not filled in, this cookie It only works on the current page!   document.cookie = c_name + "=" + escape(value) + ";path=/"+((expiredays == null) ? "": ";expires=" + exdate.toUTCString()); //If you give exiredays Assignment closes label clearance cookie }

 

2. Getting cookie s

Simple Writing:

document.write(document.cookie)

 

Function Writing:

function getCookie(name){
    var strcookie = document.cookie;//Obtain cookie Character string
    var arrcookie = strcookie.split("; ");//Division
    //Ergodic matching
    for ( var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] == name){
            return unescape(arr[1]);
        }
    }
    return "";
}

3. Delete a cookie

To delete a cookie, you can set its expiration time to a past or current time, for example:

function clearCookie(name) {  
    setCookie(name, "", -1);   //This function borrows from the above1.Set up cookie
}  

Cookies are cleaned up in two ways:

    • Clear cookie s through browser tools (third-party tools, browsers themselves have this function)
    • Clear cookies by setting the cookie's expiry date
    • Note: Deleting cookie s can sometimes cause some web pages to fail to function properly.

 

4. Check cookie s

function checkCookie() {
    var user = getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
        user = prompt("Please enter your name:", "");
        if (user != "" && user != null) {
            setCookie("username", user, 365);  
        }
    }
}

 

5. Clear up all cookie s in a domain name

function clearAllCookie() {
       var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
       if(keys) {
                    for(var i = keys.length; i--;)
                    document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
                }
            }

To be updated...

Topics: PHP Javascript Attribute Google