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 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面