File_put_contents () messing with updated array values

Sorry if the title is a bit confusing.

My results below indicate what is happening.


This question has become quite extensive, so I emphasize these two things here:

Check out my update below for a plausible reason.

Entire file: (see line 86) http://codepad.org/oIXaZZaB


Here's what happens:

I have this array that contains an integer count for each language code.

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

The key is obtained from the parameter $_GET['langCode'], always gets counted in the corresponding key $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));

, script, , $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


UPDATE:

file_put_contents() , , !

, , file_put_contents(), () , - .

?!

- ?


2:

json_encode() file_put_contents() -, " ", ($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 script ( file_put_contents). file_get_contents .


4: ()

, Safari 7 .

Firefox Chrome script.

?


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>
+4
2

. , script ( "" , "".

file_put_contents(). , - script:

<?php

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

// your code ...

script, html- :

php yourscript.php

: http://codepad.org/XhS6uEz1

, .

, php, , , .

+1

, , script. , , , , .

0

All Articles