Home > Backend Development > PHP Tutorial > setcookie 与 setrawcookie 的区别

setcookie 与 setrawcookie 的区别

WBOY
Release: 2016-06-06 20:37:57
Original
1526 people have browsed it

setcookie 与 setrawcookie 的区别

<code>setrawcookie( 'jx_lang', rawurlencode( $str ), time() + 3600 );
</code>
Copy after login
Copy after login

setrawcookie给 值 加上rawurlencode与不加的区别

回复内容:

setcookie 与 setrawcookie 的区别

<code>setrawcookie( 'jx_lang', rawurlencode( $str ), time() + 3600 );
</code>
Copy after login
Copy after login

setrawcookie给 值 加上rawurlencode与不加的区别

setcookie 手册上有一段话是这么描述的:

Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. If you don't want this, you can use setrawcookie() instead if you are using PHP 5.

也就是说 setcookie 是用了 urlencode 对 cookie 进行编码了的,而 setrawcookie 是没有的。那么根据这个,题主的问题也就转化成了 urlencoderawurlencode 有什么不同了,总的来说这两个主要的差别只是编码协议的不一样而已,根据搜索,我找到了这个地址:http://stackoverflow.com/questions/996139/urlencode-vs-rawurlencode 。这上头详细的描述了这两者的区别,你可以参考一下。主要的原因其实还是那个+空格 的编码,http://3v4l.org/15udk 在实例上你能清楚的体现区别。setrawcookie 手册上的这个说明也表明了为什么在某些场景下需要使用 urlrawencode:http://php.net/manual/zh/function.setrawcookie.php#62848

<code>$str = '123_,; abc';

setcookie('test', $str, time()+60, '/');
// value值:123_%2C%3B%20abc

setrawcookie('test1', $str, time()+60, '/');
// value值:123_,; abc

setrawcookie('test2', rawurlencode($str), time()+60, '/');
// value值:123_%2C%3B%20abc

setrawcookie('test2', encode_cookie_value($str), time()+60, '/');
// value值:123_%2C%3B%20abc
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template