如果标题有点令人困惑,请道歉.
我的结果指出了发生了什么.
这个问题变得非常广泛,所以我在这里强调这两件事:
有可能的原因,请参阅我在底部的更新.
这是发生了什么:
我有这个数组,每个语言代码包含计数整数.
$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 ?
];
更新:
当我没有使用两个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文件仍然会发生同样的错误.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)/?$index.php?lang=$1 [QSA,NC,L]