Q:

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

A:

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.

