久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP 反序列化因非編碼字符而失敗?

PHP unserialize fails with non-encoded characters?(PHP 反序列化因非編碼字符而失敗?)
本文介紹了PHP 反序列化因非編碼字符而失敗?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

$ser = 'a:2:{i:0;s:5:"héll?";i:1;s:5:"w?rld";}';//失敗$ser2 = 'a:2:{i:0;s:5:"hello";i:1;s:5:"world";}';//有效$out = 反序列化($ser);$out2 = 反序列化($ser2);打印_r($out);打印_r($out2);echo "
";

但是為什么?
我應該在序列化之前編碼嗎?怎么樣?

我使用 Javascript 將序列化字符串寫入隱藏字段,而不是 PHP 的 $_POST
在 JS 中,我有類似的東西:

function writeImgData() {var caption_arr = new Array();$('.album img').each(function(index) {caption_arr.push($(this).attr('alt'));});$("#hidden-field").attr("value", serializeArray(caption_arr));};

解決方案

unserialize() 失敗的原因:

$ser = 'a:2:{i:0;s:5:"héll?";i:1;s:5:"w?rld";}';

是因為 héll?w?rld 的長度是錯誤的,因為 PHP 本身不能正確處理多字節字符串:

echo strlen('héll?');//7echo strlen('世界');//6

但是,如果您嘗試 unserialize() 以下正確的字符串:

$ser = 'a:2:{i:0;s:7:"héll?";i:1;s:6:"w?rld";}';echo '

';打印_r(反序列化($ser));echo '</pre>';

它有效:

數組([0] =>你好[1] =>世界)

如果您使用 PHP serialize() 它應該正確計算多字節字符串索引的長度.

另一方面,如果您想使用多種(編程)語言處理序列化數據,您應該忘記它并轉向使用更標準化的 JSON 之類的東西.

$ser = 'a:2:{i:0;s:5:"héll?";i:1;s:5:"w?rld";}'; // fails
$ser2 = 'a:2:{i:0;s:5:"hello";i:1;s:5:"world";}'; // works
$out = unserialize($ser);
$out2 = unserialize($ser2);
print_r($out);
print_r($out2);
echo "<hr>";

But why?
Should I encode before serialzing than? How?

I am using Javascript to write the serialized string to a hidden field, than PHP's $_POST
In JS I have something like:

function writeImgData() {
    var caption_arr = new Array();
    $('.album img').each(function(index) {
         caption_arr.push($(this).attr('alt'));
    });
    $("#hidden-field").attr("value", serializeArray(caption_arr));
};

解決方案

The reason why unserialize() fails with:

$ser = 'a:2:{i:0;s:5:"héll?";i:1;s:5:"w?rld";}';

Is because the length for héll? and w?rld are wrong, since PHP doesn't correctly handle multi-byte strings natively:

echo strlen('héll?'); // 7
echo strlen('w?rld'); // 6

However if you try to unserialize() the following correct string:

$ser = 'a:2:{i:0;s:7:"héll?";i:1;s:6:"w?rld";}';

echo '<pre>';
print_r(unserialize($ser));
echo '</pre>';

It works:

Array
(
    [0] => héll?
    [1] => w?rld
)

If you use PHP serialize() it should correctly compute the lengths of multi-byte string indexes.

On the other hand, if you want to work with serialized data in multiple (programming) languages you should forget it and move to something like JSON, which is way more standardized.

這篇關于PHP 反序列化因非編碼字符而失敗?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 亚洲天堂精品久久 | 久久久久国产一区二区三区 | 欧美不卡 | 日本中文字幕在线视频 | 天天舔天天 | 99精品在线观看 | 超碰导航| 日韩精品不卡 | 欧美国产一区二区三区 | 日韩视频二区 | 亚洲成人av一区二区 | 岛国毛片 | 久久综合九色综合欧美狠狠 | 欧美精品中文字幕久久二区 | 国产精品欧美一区二区三区不卡 | 国产精品久久av | heyzo在线| 欧美一区二区在线观看 | 国产精品一区二区免费 | 久久综合久 | 国产夜恋视频在线观看 | 亚洲视频在线一区 | 国产精品99久久久久久久久久久久 | 成人av免费 | 亚洲欧洲视频 | 国产精品美女www爽爽爽视频 | xxxxx黄色片 欧美一区免费 | 中文日韩在线视频 | 日本成人午夜影院 | 国产欧美精品一区二区色综合朱莉 | 91一区二区三区 | 中文字幕在线视频免费视频 | 国产精品久久久久久久久久久免费看 | 欧美另类视频在线 | 久草电影网| 欧美一极视频 | 黄毛片| 国产高清久久久 | 国产精品激情 | 大伊人久久| 亚洲精品4 |