douniewei6346 2011-02-24 15:12
浏览 113
已采纳

PHP:将base64数据存储在cookie中

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
  • 写回答

1条回答 默认 最新

  • douchi7073 2011-02-24 15:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题