>  기사  >  백엔드 개발  >  setcookie 与 setrawcookie 的区别

setcookie 与 setrawcookie 的区别

WBOY
WBOY원래의
2016-06-06 20:37:571466검색

setcookie 与 setrawcookie 的区别

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

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

回复内容:

setcookie 与 setrawcookie 的区别

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

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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.