php – file_put_contents()搞乱更新的数组值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – file_put_contents()搞乱更新的数组值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5599字,纯文字阅读大概需要8分钟。
内容图文
如果标题有点令人困惑,请道歉.
我的结果指出了发生了什么.
这个问题变得非常广泛,所以我在这里强调这两件事:
有可能的原因,请参阅我在底部的更新.
整个档案:(见第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,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 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,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 0,
'nl-NL' => 1,
'fr-BE' => 1,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
$uniqueVisitors = [
'127.0.0.1' => [ 'nl-NL', 'fr-BE' ]
];
/* Result after running script with 'en-UK' langCode */
$downloads = [
'all' => [
'nl-BE' => 0,
'nl-NL' => 2,
'fr-BE' => 1,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 1,
'en' => 1 // Why is this one counted ?
],
'unique' => [
'nl-BE' => 0,
'nl-NL' => 1,
'fr-BE' => 1,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 1,
'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($uniqueVisitors));
file_put_contents('data/downloads.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,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 0,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
使用$_GET更新值[‘langCode’] =’nl-BE’:
$downloads = [
'all' => [
'nl-BE' => 1,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 1,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
当我尝试使用file_put_contents()存储更新的数组时:
$downloads = [
'all' => [
'nl-BE' => 2, // Why this increment?
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
],
'unique' => [
'nl-BE' => 1,
'nl-NL' => 0,
'fr-BE' => 0,
'fr-FR' => 0,
'de-DE' => 0,
'it-IT' => 0,
'en-UK' => 0,
'en' => 0
]
];
奇怪的是,当我使用file_put_contents时,更新的$downloads显示错误更新,当在同一个脚本运行时转储(在file_put_contents之前和之后).我甚至不必使用file_get_contents来检索错误保存的数据.
更新4 :(原因)
Safari 7出现了这个错误.
Firefox和Chrome都完美地运行此脚本.
我在哪里可以举报?
htaccess的:
没有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>
解决方法:
我也无法重现你的错误.在我看来,你的脚本被调用两次(参见’all’中的项目增加但不是’unique’.
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()搞乱更新的数组值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。