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.
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.
精彩评论