Check ip, register in array, convert to json and write to file

3

Personally I'm having a problem. I have a script in php that opens a json file, decodes it with json_decode and then checks to see if the json object exists in the current visitor's ip, if it has it, but if it does not, it registers in an array to encode json and save in the json file.

My question is that I am not able to check each ip and if I do not have ip register in the array only once. I'm using for and it repeats every action of the IF.

See the iteration code:

for ($i = 0; $i < $total_visitors; $i++){
    $ip = $decoded_json_stats[$i]->ip;

    if ($ip == $visitor_ip) {
        echo "tem cadastrado<br>";
    } else {
        echo "não tem cadastrado<br>";
    }
}

Where is the problem? I already tried with foreach but the ip already registered was registered again. See the full code of my script so far.

<?php

/* Pegar o ip do visitante e cadastra no arquivo de estatisticas
*/
$visitor_ip = $_SERVER["REMOTE_ADDR"];

$fstats = file_get_contents("stats.json");
$decoded_json_stats = json_decode($fstats);
$decoded_json_to_array = json_decode($fstats, true);
$total_visitors = count($decoded_json_stats);

$a = "127.0.0.4";

for ($i = 0; $i < $total_visitors; $i++){
    $ip = $decoded_json_stats[$i]->ip;

    if ($ip == $visitor_ip) {
        echo "tem cadastrado<br>";
    } else {
        $new_visitor_array = array("ip" => $visitor_ip);
        array_push($decoded_json_to_array, $new_visitor_array);

        $new_json = json_encode($decoded_json_to_array);

        $file = fopen("stats.json", "w");
        fwrite($file, $new_json);
        fclose($file);
        return false;
    }
}
?>

I have a file called stats.json which is where the registered ips will be. Here's how it's at the moment:

[{"ip":"127.0.0.2"},{"ip":"127.0.0.1"},{"ip":"127.0.0.4"}]

The script is in English for my preference.

Thank you!

    
asked by anonymous 09.01.2017 / 19:12

2 answers

2

Let's go through the json objects and see if any of them is the same as the visitor's ip ( $new_ip , in this case), and in this example the $jsonRaw will come from the file in your case, eg

$jsonRaw = '[{"ip":"127.0.0.6"},{"ip":"127.0.0.1"},{"ip":"127.0.0.4"},{"ip":"192.168.1.72"},{"ip":"192.168.1.73"}]';
$jsonDeco = json_decode($jsonRaw);
$new_ip = '17.0.0.1';
$found = false;
foreach($jsonDeco as $ip) {
    if($new_ip == $ip->ip) {
        echo 'tem cadastrado ' .$ip->ip. '<br>';
        $found = true;
        break;
    }
}
if(!$found) {
    $jsonDeco[] = array('ip' => $new_ip);
}
$new_json = json_encode($jsonDeco);
$file = fopen("stats.json", "w");
fwrite($file, $new_json);
fclose($file);
    
09.01.2017 / 19:33
2

You can use the array_filter function to check if the new IP is already in an array of IP's registered;

$jsonRaw = '[{"ip":"127.0.0.6"},{"ip":"127.0.0.1"},{"ip":"127.0.0.4"},{"ip":"192.168.1.72"},{"ip":"192.168.1.73"}]';
$jsonDeco = json_decode($jsonRaw, true); # Converte o json para array

$new_ip = '127.0.0.6';

$has_ip = array_filter($jsonDeco, function($indice) use ($new_ip) {
    return $indice['ip'] == $new_ip;
});

if (empty($has_ip)) {
    array_push($jsonDeco, ['ip' => $new_ip]);
    ... # grava o array $jsonDeco após realizar o json_encode no arquivo
}

The array_filter will return an array with the ips found in the list of ips, so you can check if it is empty or not, if it is then the IP is not yet registered.

    
09.01.2017 / 21:10