[Station] series of tutorials 3.1, cookie Introduction

EDITORIAL: Hello everyone, I love programming 小泽.
[Station] tutorial series is my personal experience establishment wrote the majority of their fellow citizens establishment teaching blog.
Like it Like it - a point to welcome the comments section exchanges and discussions -

cookie是站长必会的一个知识,它在本地设置、用户会话、用户标识等等方面都有作用。我们先上几个应用场景。

cookie application scenarios

1 website fiction font, color, background color settings

  We know that each time you refresh the page file will be displayed once again. If you are reading a novel website novel, the background color of the page default is white, after you put it into beige, each refresh is still white. This is not very worried? Each time you refresh should be set out.
  So, the customer put forward a demand: After solving each refresh, the initial color of the page I have become color settings, rather than the default color. Even after seven days and then when I opened this site, I still color settings.

  cookie can be achieved.

2 hidden inside the url parameter

  Under normal circumstances, after the login page to log into the homepage, url like this: http://localhost/index.php?id=123456&pw=123456But this is not safe ah, others parameters can be seen directly, so the proposed second solution.
  We value id and pw put together encrypted transmission and decrypted at the server side, url is this: http://localhost/index.php?infor=awelivbu123457khbvso little used, but as long as it can be decrypted plaintext, others will be able to crack.
  So they made a demand, I do not want to display inside the url parameter. url this way: http://localhost/index.phpthat those parameters how to save and transfer it?

  cookie can be achieved.

Introduction of the popular cookie

  Cookie is sent to the user's browser and the server locally saved a small piece of data, it will then be initiated to carry on the server and sent a request to the same server in the browser next time.
  Cookie mainly for the following three aspects:
  1, session state management (such as user login status, cart, game scores or other information to be recorded)
  2, personalization settings (such as user-defined settings, themes, etc.)
  3, View behavior tracking (tracking user behavior analysis, etc.)


【Note】:
  1. cookie stored in the user's browser. Server can set the effective time, i.e., fail over time.
  2. cookie stored in the browser, it is for the site-level. That is, you are http://localhost/under no matter which page cookie settings are stored at localhost. That is, to save the site as a unit.
  3. How to check browser cookie? In my example the qq browser. F12 to enter the Developer Tools -> Application-> cookie. As shown, I csdn screenshot.Here Insert Picture Description

Basic usage of cookie

js version

Meet the basic needs

function setCookie(key, value, iDay) {
	var oDate = new Date();
	oDate.setDate(oDate.getDate() + iDay);
	document.cookie = key + '=' + value + ';expires=' + oDate;

}
function removeCookie(key) {
	setCookie(key, '', -1);//这里只需要把Cookie保质期退回一天便可以删除
}
function getCookie(key) {
	var cookieArr = document.cookie.split('; ');
	for(var i = 0; i < cookieArr.length; i++) {
		var arr = cookieArr[i].split('=');
		if(arr[0] === key) {
			return arr[1];
		}
	}
	return false;
}

jQuery version

jQuery also encapsulates its own jquery.cookie.min.jsfile, less demand, I suggest using the above js version, the code is relatively small.

Use two scenarios to solve the beginning of the blog's cookie

1 website fiction font, color, background color settings

  Solve logic:
1, the beginning, the site is not cookie. After refreshing, to detect whether the cookie, have a cookie, then in accordance with the value of the cookie to set css page; no cookie, the default page of fonts, etc. are.
2, each time the user changes the font, color, etc., are to update the look cookie.

2 hidden inside the url parameter

  Resolution logic:
the MD5 encrypted user id and id and password stored in the cookie, each time the user performs an operation related to the database to verify the user's token is correct.

Here Insert Picture Description

Published 66 original articles · won praise 48 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43592352/article/details/104490205