白名单json和php

时间:2016-06-29 08:28:26

标签: php json whitelist

我在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它应该说成功,如果没有列入白名单,它应该说失败

1 个答案:

答案 0 :(得分:0)

使用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会返回User is whitelisted

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