JavaScript-Who and I say that PHP accepts cookies is going to involve those functions?

Source: Internet
Author: User
Tags php server
I am a business enthusiast, are more curious about what things, learn what.

I'm going to ask what this cookie is about,
Is it from the server, verify the other user name, password, return an encrypted string, and put this string to the server database.

In the future when the user logs on, there is no need to verify the ID and password, only to verify that the string and the database is right up. After a while, the string of the database is deleted.

That's what the principle should be. But what I don't understand is how this cookie was sent over from the previous paragraph, and I'll use the action to submit post and get, and I'll use Ajax to pass the number. The value is also passed by URL.

What are the relevant functions of jquery or HTML code that submitted cookies in the previous paragraph?

There are also the relevant functions from the PHP server to receive cookies, we help to provide a key word, I go to search, or simply talk about matters of caution, thank you.

Reply content:

I am a business enthusiast, are more curious about what things, learn what.

I'm going to ask what this cookie is about,
Is it from the server, verify the other user name, password, return an encrypted string, and put this string to the server database.

In the future when the user logs on, there is no need to verify the ID and password, only to verify that the string and the database is right up. After a while, the string of the database is deleted.

That's what the principle should be. But what I don't understand is how this cookie was sent over from the previous paragraph, and I'll use the action to submit post and get, and I'll use Ajax to pass the number. The value is also passed by URL.

What are the relevant functions of jquery or HTML code that submitted cookies in the previous paragraph?

There are also the relevant functions from the PHP server to receive cookies, we help to provide a key word, I go to search, or simply talk about matters of caution, thank you.

The server through the HTTP response header Set-Cookie to tell the user proxy cookie name, value, corresponding path, expiration date, etc., after the user agent (usually the browser) is saved, when the back end of the request data, the corresponding cookie will be placed in the request header sent to the server.

The front end can also be used to set cookies via JS.

So as long as the cookie is set up, the browser will automatically enclose the cookie when requested, and does not require you to submit it voluntarily.

Set Cookies in PHP

The function of setting a cookie in PHP is setcookie (there is one that is setrawcookie not commonly used).

setcookie('hello', 'world', time()+3600, '/test/', 'example.com');

This sets up a hello cookie named value that is world valid for the current time plus 3,600 seconds (that is, 1 hours), the domain name is example.com , and the path is /test/ (represents only valid on paths such as http://example.com/test/a.php this).

For specific parameters please refer to document Http://php.net/manual/zh/func ....

Server sends cookies

The server contains a header in the HTTP header of the response Set-Cookie :

Set-Cookie: hello=world; Expires=Sun, 13 Nov 2016 11:30:00 GMT; Domain=example.com; Path=/test/

Where the Expiration Time ( Expires ) is expressed using GMT, here I assume that I am a cookie set in Beijing time November 13, 2016 18:30:00 (that is, GMT November 13, 2016 10:30:00), which is valid for 1 hours.

If there are multiple cookies, there will be multiple Set-Cookie headers.

Specific reference to Http://www.cnblogs.com/hdtian ....

Browser sends cookies

The browser will save the cookie. The next time you request an address that meets the cookie criteria, the browser includes a header in the request header Cookie :

Cookie: hello=world

If there are multiple cookies or only one Cookie header, the name values of each cookie are separated by semicolons and spaces ; .

Whether you are an AJAX request or a normal post and get, the browser is sent to the server as long as the cookie is valid.

PHP Receives processing cookies

PHP parses the header information and parses the cookie into the $_COOKIE array. So we can access the hello value of the cookie named:

$_COOKIE['hello'];

Operation Cookie in JS

JS is a bit of a hassle to access cookies because there is no convenient interface to read and set cookies. We can only document.cookie manipulate cookies by doing so. For details, please refer to: https://developer.mozilla.org ....

JS is used to read the cookie directly document.cookie , but it is true and the above browser sends a cookie in the format similar to the string, is separated by semicolons and spaces of the name value pairs.

Like hello=world; PHPSESSID=web5~toqn2au0krlholat9c2c4aast3 this. We need to parse it ourselves.

JS Set the cookie is similar to the above server to send a cookie , you have to set each parameter. Like what:

document.cookie="hello=kitty; expires=Sun, 13 Nov 2016 12:00:00 GMT"

This hello changes the value and kitty extends the expiration time by half an hour.

Although it may seem that the entire value has been modified document.cookie , it does not actually affect other cookies. Our visit again document.cookie will hello=kitty; PHPSESSID=web5~toqn2au0krlholat9c2c4aast3 result in this.

To delete a cookie, just set the expiration time before the current time.

Because the HTTP design is stateless, there is a cookie state to act on HTTP . With cookie , the service side will know, who you are. phpThere are setcookie functions, there are also $_COOKIE to get cookie . cookieis passed through the header field Cookies and Set-Cookie . phpIt is also possible to set and read using the set return header and the read return header cookie cookie .

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.