Home  >  Article  >  Backend Development  >  javascript - Who can tell me how PHP accepts cookies and what functions are involved?

javascript - Who can tell me how PHP accepts cookies and what functions are involved?

WBOY
WBOYOriginal
2016-12-01 00:56:39888browse

I am a business enthusiast, and I am always curious about what to do and what to learn.

I asked what happened to this cookie.
Is it from the server that after verifying the other party's username and password, an encrypted string is returned and the string is stored in the server database.

When the user logs in in the future, there is no need to verify the ID and password. It only needs to verify whether the string and the database are correct. After a while, the string in the database will be deleted.

This should be the principle. But what I don't understand is how this cookie was sent from the previous section. I can use actions to submit posts and get, and I can also use ajax to pass numbers. The url will also be used to pass the value.

Please tell me what are the related functions of jQuery or html code that submitted cookies in the previous section.

What are the related functions for collecting cookies from the PHP server? Can you please provide a keyword and I will search for it, or simply tell me some precautions. Thank you.

Reply content:

I am a business enthusiast, and I am always curious about what to do and what to learn.

I asked what happened to this cookie.
Is it from the server that after verifying the other party's username and password, an encrypted string is returned and the string is stored in the server database.

When the user logs in in the future, there is no need to verify the ID and password. It only needs to verify whether the string and the database are correct. After a while, the string in the database will be deleted.

This should be the principle. But what I don't understand is how this cookie was sent from the previous section. I can use actions to submit posts and get, and I can also use ajax to pass numbers. The url will also be used to pass the value.

Please tell me what are the related functions of jQuery or html code that submitted cookies in the previous section.

What are the related functions for collecting cookies from the PHP server? Can you please provide a keyword and I will search for it, or simply tell me some precautions. Thank you.

The server tells the user agent the name, value, corresponding path, validity period, etc. of the cookie through the HTTP response header Set-Cookie. After the user agent (usually the browser) saves it, when requesting data from the backend, it The corresponding cookie will be placed in the request header and sent to the server.

The front end can also set cookies through JS.

So as long as the cookie is set, the browser will automatically attach the cookie to the request, and you do not need to actively submit it.

Set cookies in PHP

The function to set cookies in PHP is setcookie (there is also setrawcookie which is not commonly used).

<code class="php">setcookie('hello', 'world', time()+3600, '/test/', 'example.com');</code>

In this way, a file named hello with a value of world, a validity period of the current time plus 3600 seconds (that is, 1 hour), a domain name of example.com, and a path of /test/ (represents Cookies that can only be valid under paths such as http://example.com/test/a.php).

Please refer to the document http://php.net/manual/zh/func... for specific parameters.

The server sends cookies

The server will include a Set-Cookie header in the response HTTP header:

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

The expiration time (Expires) is represented by GMT. Here I assume that I am at 18:30:00 on November 13, 2016, Beijing time (that is, 10:30:00 on November 13, 2016 GMT) The cookie set is valid for 1 hour.

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

For details, please refer to http://www.cnblogs.com/hdtian... .

The browser sends cookies

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

<code>Cookie: hello=world</code>

If there are multiple cookies or only one Cookie header, use semicolons and spaces ; to separate the name and value pairs of each cookie.

Whether you are making an AJAX request or ordinary POST and GET, as long as the cookie is valid, the browser will send it to the server.

PHP receives and processes cookies

PHP will parse the header information and parse the cookie into the $_COOKIE array. We can access the value of the cookie named hello like this:

<code class="php">$_COOKIE['hello'];</code>

Manipulate cookies in JS

Accessing cookies in JS is a bit troublesome because there is no convenient interface to read and set cookies. We can only manipulate cookies through document.cookie. For details, please refer to: https://developer.mozilla.org... .

JS中读取cookie直接用document.cookie,但是得到的确实和上面浏览器发送cookie中的格式类似的字符串,就是用分号和空格隔开的名值对。

比如hello=world; PHPSESSID=web5~toqn2au0krlholat9c2c4aast3这样的。我们需要自己解析。

JS中设置cookie则是和上面服务器发送cookie类似,都要设置各个参数。比如:

<code class="javascript">document.cookie="hello=kitty; expires=Sun, 13 Nov 2016 12:00:00 GMT"</code>

这样就把hello的值改成了kitty,并且把过期时间延长了半个小时。

虽然看起来好像修改了document.cookie整个值,但实际上并不会影响到其他cookie。我们再次访问document.cookie将得到hello=kitty; PHPSESSID=web5~toqn2au0krlholat9c2c4aast3这样的结果。

要删除一个cookie,只要把过期时间设置在当前时间之前就行了。

因为HTTP设计是无状态的,所以就有了cookie来充当HTTP的状态。有了cookie,服务端就知道,你是谁了。phpsetcookie函数,还有$_COOKIE来获取cookiecookie是通过头部字段CookiesSet-Cookie来传递的。所以php也可以使用设置返回头和读取返回头来设置cookie和读取cookie

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn