douchuang1852 2011-10-26 14:43
浏览 109
已采纳

PHP从64位到32位的非序列化整数

I have been trying to unserialize on a 32-bit server an object that was serialized on a 64-bit server. I have isolated my problem to an integer in the object. Here is a small reproduction of the problem.

On a 64-bit machine:

$i = serialize('20110510134021'); //i:20110510134021;

On a 32-bit machine:

$i = unserialize('i:20110510134021;');

Gives the error

Notice: unserialize(): Error at offset 0 of 16 bytes

Now I understand that those serialization method shouldn't be used for cross system data transmition. However, we are merely trying to migrate the data to another system, not use it actively for transfers. This is a one time thing.

I would think that this might be due to integer overflow, but even on the 32-bit server I can do something like

$i = 20110510134021;
echo $i;

And it will work fine. I'm guessing PHP integer types scale to some double type or something like that. But why doesn't it do that when it unserializes?

How can I unserialize those objects? If I can't, is there any way to convert them to something else? Finally, has anyone written a deserialize method in PHP itself? Or has details on the protocol? I could use that and have a custom case for those integers.

Thanks.

Note: I do not have access to the original data, only the serialized result.

  • 写回答

3条回答 默认 最新

  • duanjiaopi8218 2011-10-26 15:00
    关注

    The max integer on 32 bit system is 4294967296; $i = 20110510134021; works because PHP converts the variable to double.

    So replace i with d

    $i = unserialize('d:20110510134021;');
    

    If you run this script, you will see the correct represenation of the variable on the system you are running (d: on 32 bit system, i: on 64 bit system):

    $int = 20110510134021;
    var_dump(serialize($int));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度