The meaning of cookies
- Suppose that after I log in, a piece of information is saved in my cookie. The expiration time of this information is 7 days.
- No matter if I close the browser or not, as long as I don’t clear the cache, the cookie will always be there.
- Expires in 7 days
- When I log in:
- Let me first check if there is any information I saved in the cookie
- If there is, then directly follow the login to complete the processing
- If not, it will be handled as not logged in
- As long as I log in once:
- This login information can be saved within 7 days
- There is no need to log in again within 7 days, every time you open the website, it will be processed according to the login completion.
- After 7 days, you need to log in again
- That is to realize the 7-day login-free function
What is a cookie
cookie
Is a location where data is stored in the form of a string
- Every HTTP request will carry the cookie in the request header to the server
- Every HTTP response will carry a cookie in the response header to the client
- In other words, the cookie does not need to be manually set by us, it will automatically move data between the client and the server
- We just need to set the content of the cookie.
Storage form of cookie
- The cookie is stored in
key=value
the form of a string and appears in the string in the form of
- Each
key=value
is a piece of data
;
Split between multiple data
`a=100; b=200; c=300;`
Characteristics of cookies
- Stored by domain name: that is, whoever sets it can read it
- Timeliness: that is, there is an expiration time,
- The default is the session level (that is, it expires when the browser is closed)
- Can be set manually
- Storage size is limited: generally 4kb or about 50
- Both front and back ends can be operated:
- Front-end operation through js
- The backend operates through the backend language
- Request automatic carry:
- When there is content in your cookie space
- As long as it is any request under the current domain name, it will automatically carry the cookie and put it in the request header
- How much is automatically carried in the cookie
- If there is no content in the cookie, it will not be carried
JS operation cookie
Add cookie
- grammar:
document.cookie = "key=value";
- Note: You can only set one cookie at a time, if you want to set two, you have to write twice
- Timeliness of setting cookies
- When not set, the default is session aging (disappear after closing the browser)
- Setting the timeliness of the cookie is to modify the cookie
document.cookie = "key=value; 修饰信息1=修饰的值1;修饰信息2=修饰的值2"
- Set expiration time:
document.cookie = "key=value; expires = 时间对象"
- Set a valid path:
document.cookie = "key=value; path=路径"
(If path is not set, the default is the current directory path)
- Timing of setting cookies
- What we get using new Date() is the time of our terminal, which is the time in the +8 time zone
- Suppose we get 9:30 with new Date()
- But the time of this cookie in our terminal is 17::30
- If you want the cookie to expire at the current time
- Then the terminal time you get is -8 hours
document.cookie = 'a=100';
document.cookie = 'b=hello';
var date = new Date('2022/2/12');
document.cookie = "c=200; expires="+date;
document.cookie = "d=300;path=/";
var date1 = new Date();
date1.setTime(date1.getTime()-8*60*60*1000+5*1000);
document.cookie = "e=200;expires="+date1;
document.cookie = 'f=100; expires=Thu,18 Dec 2043 12:00:00 GMT';
console.log(document.cookie);
Delete cookie
- Cookies cannot be deleted
- If you don't want this cookie anymore
- Then set the expiration time of this cookie to before the current time, then delete
document.cookie = "name=zhangsan";
var date = new Date();
date.setTime(date.getTime()-8*60*60*1000-1);
document.cookie = "name=zhangsan;path=/;expires="+date;
Get cookie
- js gets the cookie:
document.cookie
- He can't get a single piece of data, he can only get all cookies
document.cookie = "aa=100";
document.cookie = "bbb=200";
document.cookie = "c=300";
document.cookie = "ac=60";
console.log(document.cookie);
var key = "ac";
var result;
var str = document.cookie;
var arr = str.split("; ");
for(var i=0; i<arr.length; i++){
var tempArr = arr[i].split("=");
if(tempArr[0]==key){
result = tempArr[1];
break;
}
}
console.log(result);
JS operation cookie package
- Because there is no special method for adding, deleting, checking and modifying cookies in js
- So we need to encapsulate a method ourselves
Set cookie
function setCookie(key, value, expires){
const time = new Date();
time.setTime(time.getTime()-1000*60*60*24*8+expires);
document.cookie = `${
key}= ${
value}; expires=${
time}`;
}
Delete cookie
function delCookie(name){
setCookie(name,1,-1);
}
Get cookie
function getCookie(key){
const cookieArr = document.cookie.split('; ');
let value = '';
cookieArr.forEach(item=>{
if(item.split('=')[0] === key){
value = item.split('=')[1];
}
})
return value;
}
PHP manipulation cookie
Add cookie
- grammar:
setcookie(key,value,过期时间);
- key: the name of the cookie to be set
- value: the value of the cookie to be set
- Expiration time: You can not write, the default is the session time limit
- The backend obtains the time object using the time() method
- What you get is the world standard time in seconds
- 15 seconds after the current time: time()+15
setcookie('php','200',time()+15);
Modify cookie
- Backend modify cookie
- Also use the setcookie method
- Just set it up again
setcookie('a',100);
setcookie('a',200);
Delete cookie
- Delete cookies on the backend
- Also use the setcookie method
- It’s just that the cookie’s expiration time is set to before the current time
setcookie('a','hello',time()-1);
Get cookie
- Get cookies from the backend
- When the front end sends a request, the cookie will be carried in the request header
- PHP has a predefined variable called
$_COOKIE
- Is an associative array, similar to
$_GET/$_POST
- Stored in it is cookie information one by one
- Which one you want, just
$_COOKIE
take it out
Code demo
<?php
setcookie('php',200,time()+15);
setcookie('php',100);
$php = $_COOKIE['php'];
setcookie('php',100,time()-1);
?>