白名单 json 和 php

Whitelist json and php

我正在 php 和 json 中创建一个用户 ID 白名单,但它不会工作,所以它应该是这样的

{ "users": { "ANID": { "ANID": "true" }, 
"97594568": { "isvalid": "true" }, 
"ANID": { "isvalid": "true" }, 
"ANID": { "isvalid": "true" }, 
"ANID": { "isvalid": "true" }, 
"ANID": { "isvalid": "true" }, 
"ANID": { "isvalid": "true" } } } 

如果 你去 whitelist.php?uid=ANID 它应该说成功,如果没有列入白名单它应该说失败

使用array_key_exists:

whitelist.json:

{
  "users": {
    "0001": {
      "isvalid": "true"
    },
    "0002": {
      "isvalid": "true"
    },
    "0003": {
      "isvalid": "true"
    },
    "0004": {
      "isvalid": "true"
    },
    "0005": {
      "isvalid": "true"
    }
  }
}

whitelist.php

<?php

$data = file_get_contents('whitelist.json');
$json = json_decode($data, true);

if (array_key_exists($_GET['uid'], $json['users'])) {
    echo 'User is whitelisted';
} else {
    echo 'User is NOT whitelisted';
}

whitelist.php?uid=0001 会 return User is whitelisted

whitelist.php?uid=0006 会 return User is NOT whitelisted