首页 > setcookie 与 setrawcookie 的区别

setcookie 与 setrawcookie 的区别

setcookie 与 setrawcookie 的区别

setrawcookie( 'jx_lang', rawurlencode( $str ), time() + 3600 );

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


$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

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

【热门文章】
【热门文章】