I am a business enthusiast and I am curious about what to learn. I will ask what is the cookie, whether it is an encrypted string that is returned from the server after verifying the user name and password of the other party and saving the string to the server database. When users log on Later... I am a business enthusiast and I am curious about what to learn.
I will ask how this cookie is going,
Is it from the server? after verifying the user name and password of the other party, an encrypted string is returned and saved to the server database.
When a user logs on, the user does not need to verify the id and password, but verifies that the string and database are correct. After a while, the database string is deleted.
That's how it works. But what I don't understand is how this cookie was sent in the previous section. I will use action to submit post and get, and I will also use ajax to pass data. The url will also be used to pass the value.
Which functions are related to jQuery or html code that submit cookies in the previous section.
What other functions are related to cookie receiving from the php Server? please help me provide a keyword. I will search for it, or I will give you some notes. thank you.
Reply content:
I am a business enthusiast and I am curious about what to learn.
I will ask how this cookie is going,
Is it from the server? after verifying the user name and password of the other party, an encrypted string is returned and saved to the server database.
When a user logs on, the user does not need to verify the id and password, but verifies that the string and database are correct. After a while, the database string is deleted.
That's how it works. But what I don't understand is how this cookie was sent in the previous section. I will use action to submit post and get, and I will also use ajax to pass data. The url will also be used to pass the value.
Which functions are related to jQuery or html code that submit cookies in the previous section.
What other functions are related to cookie receiving from the php Server? please help me provide a keyword. I will search for it, or I will give you some notes. thank you.
The server uses the HTTP response headerSet-CookieTo inform the user of the proxy cookie name, value, corresponding path, validity period, etc. after the user proxy (usually a browser) is saved, when you request data from the backend, the corresponding cookie is put in the request header and sent to the server.
The front-end can also use JS to set cookies.
Therefore, after setting the cookie, the browser will automatically attach the cookie to the request, and you do not need to submit the cookie manually.
Set cookie in PHP
The Cookie setting function in PHP issetcookie(There is anothersetrawcookieIs not commonly used ).
setcookie('hello', 'world', time()+3600, '/test/', 'example.com');
In this way,helloThe value isworld, Valid for the current time plus 3600 seconds (that is, 1 hour), the domain name isexample.com, Path is/test/(It indicates that onlyhttp://example.com/test/a.php.
For specific parameters, see the document http://php.net/manual/zh/func ....
Server sends cookie
The server will includeSet-CookieHeader:
Set-Cookie: hello=world; Expires=Sun, 13 Nov 2016 11:30:00 GMT; Domain=example.com; Path=/test/
The expiration time (Expires) Is expressed in GMT. here, we assume that I set the Cookie at 18:30:00 (November 13, 2016 10:30:00 GMT) on January 1, November 13, 2016, Beijing time. the Cookie is valid for 1 hour.
If multiple cookies exist, multipleSet-CookieHeader.
Specific can refer to the http://www.cnblogs.com/hdtian ....
Cookie sent by the browser
The browser saves the Cookie. The next time you request a Cookie-compliant address, the browser will includeCookieHeader:
Cookie: hello=world
If there are multiple cookies, there is still only oneCookieHeader. the names and values of each cookie use semicolons and spaces.; .
Whether you are an AJAX request or a common POST or GET request, the browser will send the cookie to the server as long as the cookie is valid.
PHP receives and processes cookies
PHP will parse the header information and parse the cookie$_COOKIEArray. We can accesshelloThe cookie value:
$_COOKIE['hello'];
Cookie operations in JS
It is a little troublesome to access cookies in JS because there is no convenient interface to read and set cookies. We can onlydocument.cookieTo operate on cookies. For more information, see: https://developer.mozilla.org ....
Directly usedocument.cookieBut the result is trueCookie sent by the browserIs a name-value pair separated by semicolons and spaces.
For examplehello=world; PHPSESSID=web5~toqn2au0krlholat9c2c4aast3This is the case. We need to resolve it ourselves.
Set the cookie in JSServer sends cookieSimilarly, you must set parameters. For example:
document.cookie="hello=kitty; expires=Sun, 13 Nov 2016 12:00:00 GMT"
In this wayhelloThe value is changedkittyAnd the expiration time is extended by half an hour.
Although it seems to have been modifieddocument.cookieBut it does not affect other cookies. Visit againdocument.cookieWill gethello=kitty; PHPSESSID=web5~toqn2au0krlholat9c2c4aast3Such a result.
To delete a cookie, you only need to set the expiration time before the current time.
BecauseHTTPThe design is stateless, so there iscookieTo actHTTP. WithcookieThe server knows who you are.phpYessetcookieFunction, and$_COOKIETo obtaincookie.cookieIs through the header fieldCookiesAndSet-Cookie. SophpYou can also set the return header and read the return header.cookieAnd readcookie.