問題描述
第一;在我的開發(fā)服務器(本地主機;OSX 上的默認 XAMPP)上一切正常,但當我將完全相同的代碼(和數(shù)據(jù))部署到臨時服務器(在 Redhat 上托管 Apache2)時,它會中斷.
First; On my development server (localhost; default XAMPP on OSX) everything works fine, though when I deploy the exact same code (and data) to the staging server (managed Apache2 on Redhat) it breaks.
我正在使用 Zend_Cache 使用文件后端和自動序列化緩存一些數(shù)據(jù).原始數(shù)據(jù)中使用的特殊字符顯示正常,但當它們從緩存中加載時,它們都是亂碼.
I'm caching some data using Zend_Cache using the File backend and auto-serialization. Special characters used in the original data display fine, though when they are loaded from cache they're all garbled up.
有人知道嗎?
附注.我正在尋找一種方法來了解臨時服務器上可能出現(xiàn)的錯誤",而不是只是一種解決方法.什么可能會搞砸?
PS. Instead of just a workaround, I'm looking for a way to understand what might go "wrong" on the staging server. What could possibly mess this up?
更新我緩存的數(shù)據(jù)是 UTF-8 編碼的.
UPDATE The data I'm caching is UTF-8 encoded.
更新在查看原始緩存文件(序列化數(shù)組的)時,我看到了一個很大的不同;當暫存服務器上緩存的(相同)數(shù)據(jù)確實顯示換行符時,我的本地主機上緩存的數(shù)據(jù)不顯示換行符.
UPDATE When looking at the raw cache files (of a serialized array) there i See one big difference; The data cached on my localhost shows no newlines when the (identical) data cached on the staging server does show newlines.
更新本地服務器運行 PHP 5.3
,臨時服務器運行 PHP 5.2.10
UPDATE
Local server runs PHP 5.3
, staging server runs PHP 5.2.10
更新在 Zend FW 1.10.8 上運行
UPDATE Running on Zend FW 1.10.8
推薦答案
我和你的情況幾乎一模一樣,
I have almost identical state like you ,
開發(fā)機器windows + php 5.3
development machine is windows + php 5.3
開發(fā)機為Linux + php 5.2.14
development machine is Linux + php 5.2.14
ZF 版本是 1.10
ZF version is 1.10
我唯一的區(qū)別是:我曾經(jīng)在引導類中添加 mb_internal_encoding("UTF-8");
the only difference i had is : i used to add mb_internal_encoding("UTF-8");
in the bootstrap class
僅供參考,我曾經(jīng)從數(shù)據(jù)庫中緩存所有編碼的 UTF8 文本(阿拉伯語)當我打開文件時,我看到了預期的阿拉伯語文本.
FYI , I used to cache text (arabic language ) from database all encoded UTF8 when i open the file i see the arabic text as expected .
更新:1- 這是我完整的 initCache 函數(shù),只是為了清楚起見
UPDATE : 1- here is my complete initCache function just to make it clear
public function _initCache() {
mb_internal_encoding("UTF-8");
$frontendOptions = array(
'automatic_serialization' => TRUE,
'lifetime' => 86400
);
$backendOptions = array(
'cache_dir' => APPLICATION_PATH . "/configs/cache/",
///'cache_dir' => sys_get_temp_dir(),
);
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
Zend_Registry::set("cache", $cache);
}
說明:1-任何早于 PHP 6 的 php 版本都沒有對 UTF-8 的本機支持,https://stackoverflow.com/questions/716703/what-is-coming-in-php-6
Explanation : 1-Any php version earlier than PHP 6 doesn't have native support for UTF-8 , https://stackoverflow.com/questions/716703/what-is-coming-in-php-6
2-使 php 5.3 或 5.2 使用 ICONV 處理 UTF8或 MB_STRING
2-making php 5.3 or 5.2 deal with UTF8 by using ICONV or MB_STRING
只需使用 var_dump(mb_internal_encoding());
你可以在內(nèi)部使用 ISO-8859-1 告訴 php,
you can tell that php using ISO-8859-1 internally ,
你可以通過var_dump(mb_internal_encoding("UTF-8"));
它會輸出真(它成功覆蓋內(nèi)部編碼)
it would output true (it success to override the internal encoding )
老實說,我不知道是否有更好的解決方案或如何不好是嗎??,
to be honest i don't know if there is better solution or how bad it is ?? ,
如果你有更好的我會很樂意采用它:)
if you had any better i would be happy to adopt it :)
更新 2如果您不想使用該功能,打開這個文件 "Zend/Cache/Backend/File.php"
并轉(zhuǎn)到第 976 行改變這個:
UPDATE 2
in case you don't want to use that function ,
open this file "Zend/Cache/Backend/File.php"
and go to the line 976
change this :
protected function _filePutContents($file, $string)
{
$result = false;
$f = @fopen($file, 'ab+');
if ($f) {
if ($this->_options['file_locking']) @flock($f, LOCK_EX);
fseek($f, 0);
ftruncate($f, 0);
$tmp = @fwrite($f, $string);
if (!($tmp === FALSE)) {
$result = true;
}
@fclose($f);
}
@chmod($file, $this->_options['cache_file_umask']);
return $result;
}
成為這樣:
protected function _filePutContents($file, $string)
{
$string = mb_convert_encoding($string , "UTF-8" , "ISO-8859-1"); // i didn't test it , use it at your own risk and i'd rather stick with the first solution
$result = false;
$f = @fopen($file, 'ab+');
if ($f) {
if ($this->_options['file_locking']) @flock($f, LOCK_EX);
fseek($f, 0);
ftruncate($f, 0);
$tmp = @fwrite($f, $string);
if (!($tmp === FALSE)) {
$result = true;
}
@fclose($f);
}
@chmod($file, $this->_options['cache_file_umask']);
return $result;
}
我沒有手動測試,但它應該按預期工作
i didn't test manually but it should work as expected
很高興它有所幫助!
這篇關于Zend_Cache:加載緩存數(shù)據(jù)后,字符編碼好像亂了的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!