Php 文件\u put\u contents()与更新的数组值混淆

Php 文件\u put\u contents()与更新的数组值混淆,php,arrays,Php,Arrays,如果标题有点混乱,我道歉 我下面的结果指出了正在发生的事情 这个问题变得相当广泛,所以我在这里强调这两件事: 有关可能的原因,请参阅底部的myupdate 整个文件:(参见第86行) 下面是发生的事情: 我有一个数组,它包含每个语言代码的计数整数 $downloads = [ 'all' => [ 'nl-BE' => 0, 'nl-NL' => 0, 'fr-BE' => 0, 'fr-FR'

如果标题有点混乱,我道歉

我下面的结果指出了正在发生的事情


这个问题变得相当广泛,所以我在这里强调这两件事:

有关可能的原因,请参阅底部的myupdate

整个文件:(参见第86行)


下面是发生的事情:

我有一个数组,它包含每个语言代码的计数整数

$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
    ]
];

$\u 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
但这似乎也是随机发生的

现在你可能会认为这是由于语言代码用作,但我也使用了零索引键,结果是一样的

例如:

/*  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行)


更新:

/*  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\u put\u contents()
函数将更新后的数据保存回他们的文件时,只需进行一些测试和转储,整个计数就可以按预期工作

因此,这两个用于保存(正确)更新数据的
file\u put\u contents()

但是怎么做

有人对这种意外行为有更好的理解吗


更新2:

/*  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 ?
];
当我在我的
文件\u put\u contents()
函数中省略
json\u encode()
时,我得到了“数组到字符串转换”错误,两次用于保存计数数据(
$downloads
):


那为什么那个人被处决了两次??里面连一个环都没有


更新3:

/*  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 ?
];
我的JSON编码的
$downloads
数据正确显示了更新后的值

但是,只要我使用
file\u put\u contents()
来存储更新后的
$downloads
数据,它就会将更新后的值弄乱。即使我在使用
文件内容之前/之后转储更新的数据,它也会显示出混乱

初始
$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
    ]
];
使用
$\u 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\u put\u contents()存储更新的数组时

奇怪的是,当我使用
file\u put\u contents
时,更新的
$downloads
在同一个脚本运行期间(在
file\u put\u contents
之前和之后)转储时显示更新错误。我甚至不必使用
file\u get\u contents
来检索错误保存的数据


更新4:(原因)

看来是Safari 7导致了这个错误

Firefox和Chrome都完美地运行了这个脚本

我在哪里可以报告


htaccess:

/*  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 ?
];
如果没有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>

重新启动发动机
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-L
重写规则^(+)/?$index.php?lang=$1[QSA,NC,L]

我也无法重现您的错误。在我看来,您的脚本被调用了两次(请参阅'all'中的项是递增的,但在'unique'中不是递增的)

file\u put\u contents()
没有问题。您可以通过在脚本中添加类似的内容来验证这一点:

<?php

mail('you@domain.tld', 'script executed at '.time(), '');

// your code ...
这是我的密码:

您将看到它的行为是正确的


此外,您可以禁用一些,但我认为这不是问题所在。

查看服务器访问日志,看看是否可以确定对脚本的第二次调用的来源。我已经安装了类似的插件,调试器或检查页面加载速度的插件可以再次调用页面。

无法重现该问题使用您的代码。@CBroe这是整个文件:更新了整个文件,请参见第86行:标记的
文件内容()
在您的更新1和更新2中不会执行两次。您收到了两次错误消息,因为
$downloads
包含2个子数组,并且它们都无法转换为字符串。如果该数组包含3个子数组,您将收到3次错误消息,您可以尝试一下。至于您最初的问题,我是也无法重现您的错误,而且我在您的示例程序中也没有看到任何基本缺陷。错误的来源必须在其他地方。我的脚本怎么可能被调用两次?这毫无意义。我甚至在更新部分的末尾使用了
exit;
。一旦我使用了
file\u put\u contents
,我更新的数组保存不正确更新的值。如果不使用
文件内容,它将转储正确更新的值。整个脚本将被调用两次(以及退出)。这就像在浏览器中按f5。请在终端中运行它。它确实在终端中运行良好。但我完全不知道我的脚本如何在本地主机(以及web上的另一台服务器)上运行多次,以及