【问题标题】:Whitelist json and php [closed]白名单json和php [关闭]
【发布时间】:2016-10-31 20:14:29
【问题描述】:

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

【问题讨论】:

  • 你能分享一些你试过的PHP代码吗?
  • $value) { if (!is_array($value)) { echo $key . '=>' 。 $价值。 '
    '; } else { foreach ($value as $key => $val) { echo $key . '=>' 。 $val 。 '
    '; } } } ?>

标签: php json whitelist


【解决方案1】:

使用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

【讨论】:

  • 它很有效,你帮了我很多
  • 如果我的回答解决了您的问题,请点击勾选接受它作为答案
猜你喜欢
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 2012-03-15
  • 2012-04-22
  • 2018-01-10
  • 2019-06-25
  • 2010-11-18
相关资源
最近更新 更多