开发者

PHP: Storing base_64 data in cookies

I need to store some data in a cookie. The data goes through mcrypt encryption and then base64 to prevent any strange characters being used.

Storing the encrypted & encoded string in a session and reading it back & decrypting/decoding works perfectly. Doing the same to a cookie fails - completely gibberish text is returned.

Obviously this is due to the fact that the mcrypt-decrypt function call receives improper data, making me to believe that the contents of my cookie are affected in one way or another.

What on earth is PHP doing behind the scenes to my cookie data?

Code:

(Note that the mcrypt-functions are represented below as plain function call for readability )

$string = base64_encode( mcrypt_encrypt( 'A string' ) );
setcookie('string', $string, time()+3600);
$_SESSION['string'] = $string;

$sessiondata = base64_decode( mcrypt_decrypt($_SESSION['string']); // A string!
$cookiedata = base64_decode(开发者_如何学运维 mcrypt_decrypt($_COOKIE['string']) ); // Completely gibberish


Try calling urlencode() on your string before sending it to the cookie -- I seem to recall that PHP automatically does a urldecode() on the data when making it available in $_COOKIE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜