0

I have a php script that is supposed to assign virtual names to ip addresses.

<?php
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
$read = file_get_contents("vnames.json");
$json = json_decode($read);
var_dump($_SERVER["REMOTE_ADDR"]);
if(!array_key_exists($_SERVER["REMOTE_ADDR"], $json)){
    $json[$_SERVER["REMOTE_ADDR"]] = generateRandomString();
    $read = json_encode($json);
    echo "This if statement is true!";
    file_put_contents("vnames.json", $read);
}
?>

Inside names.json, there are is only a pair of empty brackets.

So, I've figured out that !array_key_exists($_SERVER["REMOTE_ADDR"], $json) is false. But, I'm sure that names.json does not contain my IP address. I've assumed that this is is what's being evaluated:

<?php
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
$read = file_get_contents("vnames.json");//blank
$json = json_decode($read);//null
var_dump($_SERVER["REMOTE_ADDR"]);//My ip
if(!array_key_exists($_SERVER["REMOTE_ADDR"], $json)/*false*/){
    $json[$_SERVER["REMOTE_ADDR"]] = generateRandomString();
    $read = json_encode($json);
    echo "This if statement is true!";
    file_put_contents("vnames.json", $read);
}
?>

But in that case, file_get_contents is not working correctly. Please help!

6
  • var_dump($read); what does it say? Commented May 28, 2021 at 16:38
  • There may be a permissions issue with accessing the file, Commented May 28, 2021 at 16:43
  • array_key_exists only checks the first dimension of the array, so you need to show the data structure of $json to see how deep it goes. Commented May 28, 2021 at 17:00
  • var_dump($read) Reads what it should read. string(2) "{}" Commented May 29, 2021 at 17:22
  • the value of $json is a one dimensional associative array. It reads, {}. Commented May 29, 2021 at 17:23

1 Answer 1

2

array_key_exists is only for arrays, while the $json variable contains an object.

Either change $json to be an array or use property_exists for the object.

Example to transform $json into an array

$json = json_decode($read, true);
Sign up to request clarification or add additional context in comments.

2 Comments

I tried both of those things. It didn't work. I'm pretty sure $json is an associative array, but I'm not sure. I tried var_dumping the ``` array_key_exists($_SERVER["REMOTE_ADDR"], $json) ```, and It returned NULL, instead of a boolean.
I rewrote my entire code and it worked. Maybe I misspelled something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.