脚本宝典收集整理的这篇文章主要介绍了php – file_put_contents()搞乱更新的数组值,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我的结果指出了发生了什么.@H_512_7@
这个问题变得非常广泛,所以我在这里强调这两件事:
整个档案:(见第86行)http://codepad.org/oIXaZZaB
这是发生了什么:
$downloads = [ 'all' => [ 'nl-BE' => 0,'nl-NL' => 0,'fr-BE' => 0,'fr-FR' => 0,'de-DE' => 0,'IT-iT' => 0,'en-UK' => 0,'en' => 0 ],'unique' => [ 'nl-BE' => 0,'en' => 0 ] ];
从$_GET [‘langCode’]参数中检索到的密钥总是计入$downloads [‘all’]中的相应密钥.
我还检查访问者是否是唯一的,并且之前已经使用过相同的$langCode.如果他之前没有使用相同的$langCode,则计数也会添加到$downloads [‘unique’]中的相应键中.
这是通过以下代码完成的:
$uniqueVisitors = [ '127.0.0.1' => [ 'en-UK' ] ]; if ($uniqueVisitors == null) { $uniqueVisitors = [ $ipNew => [] ]; } $countUnique = 1; /* Check Unique Visitors */ if (isset($uniqueVisitors[$ipNew])) { if (in_array($langCode,$uniqueVisitors[$ipNew])) { $countUnique = 0; } else $uniqueVisitors[$ipNew][] = $langCode; } else $uniqueVisitors[$ipNew] = [ $langCode ]; /* Update Data */ $downloads['all'][$langCode]++; if ($countUnique) { $downloads['unique'][$langCode]++; } /* Save Data */ file_put_contents('data/unique-visitors.json',json_encode($uniqueVisitors)); file_put_contents('data/downloads.json',json_encode($downloads));
现在奇怪的是,当我运行脚本时,有时会计算多个键,即使$langCode只包含一个键(例如’nl-NL’)
使用’en-UK’时,’en’也经常被计算在内.
“fr-FR”和“fr-BE”也是如此.
‘nl-NL’和’nl-BE’.
它主要发生在使用的langCode计数仍为0时
但它似乎也是以随机顺序发生的.
现在您可能认为这是由于langCodes用作键,但我也使用了零索引键,结果相同!
例如:
/* Retrieved First From JSON file */ $downloads = [ 'all' => [ 'nl-BE' => 0,'nl-NL' => 2,'fr-BE' => 1,'nl-NL' => 1,'en' => 0 ] ]; $uniqueVisitors = [ '127.0.0.1' => [ 'nl-NL','fr-BE' ] ]; /* Result after running script with 'en-UK' langCode */ $downloads = [ 'all' => [ 'nl-BE' => 0,'en-UK' => 1,'en' => 1 // Why is this one counted ? ],'en' => 1 // Why is this one counted ? ] ]; $uniqueVisitors = [ '127.0.0.1' => [ 'nl-NL','fr-BE','en','en-UK' ] // 'en' Stored as well ? ];
整个文件:(见第86行)http://codepad.org/oIXaZZaB
更新:
当我没有使用两个file_put_contents()函数将更新的数据保存回他们的文件,只是做一些测试和转储时,整个计数确实按预期工作!
因此,出现这两个file_put_contents()函数,用于保存(正确)更新的数据,以某种方式干扰,并在执行时更改一些计数.
但是如何?!
有人对这种意想不到的行为有了更好的理解吗?
更新2:
当我在我的file_put_contents()函数中省略json_encode()时,我得到“Array to String conversion”错误,两次用于保存我的计数数据($downloads):
Notice: Array to string conversion in /Applications/MamP/htdocs/PRoject/downloads/downloads.PHP on line 89 Notice: Array to string conversion in /Applications/MAMP/htdocs/project/downloads/downloads.PHP on line 90 Notice: Array to string conversion in /Applications/MAMP/htdocs/project/downloads/downloads.PHP on line 90
file_put_contents('data/unique-visitors.json',json_encode($downloads)); // This one is called twice!
那么为什么那个被执行两次?那里甚至没有一个循环.
更新3:
我的JSON编码$downloads数据正确显示更新的值.
但是一旦我使用file_put_contents()来存储更新的$downloads数据,它就会与更新的值混淆.即使我在使用file_put_contents之前/之后转储了更新的数据,它也显示出混乱.
初始$downloads数组:
$downloads = [ 'all' => [ 'nl-BE' => 0,'en' => 0 ] ];
使用$_GET更新值[‘langCode’] =’nl-BE’:
$downloads = [ 'all' => [ 'nl-BE' => 1,'unique' => [ 'nl-BE' => 1,'en' => 0 ] ];
当我尝试使用file_put_contents()存储更新的数组时:
$downloads = [ 'all' => [ 'nl-BE' => 2,// Why this increment? 'nl-NL' => 0,'en' => 0 ] ];
奇怪的是,当我使用file_put_contents时,更新的$downloads显示错误更新,当在同一个脚本运行时转储(在file_put_contents之前和之后).我甚至不必使用file_get_contents来检索错误保存的数据.
更新4 :(原因)
Safari 7出现了这个错误.
Firefox和Chrome都完美地运行此脚本.
我在哪里可以举报?
htaccess的:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l rewriterule ^(.+)/?$index.PHP?lang=$1 [QSA,NC,L] </IfModule>
file_put_contents()没有问题.您可以通过在脚本中添加以下内容来验证这一点:
<?PHP mail('you@domain.tld','script executed at '.time(),''); // your code ...
我建议复制你的脚本,删除所有的HTML代码并在带有虚拟数据的终端中执行它:
PHP yourscript.PHP
这是我的代码:http://codepad.org/XhS6uEz1
你会发现它的行为正确.
另外你可以禁用一些php module that could be responsible for such bugs,但我认为这不是问题.
以上是脚本宝典为你收集整理的php – file_put_contents()搞乱更新的数组值全部内容,希望文章能够帮你解决php – file_put_contents()搞乱更新的数组值所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。