Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created March 15, 2020 13:19
Show Gist options
  • Save PJZ9n/e116bb05d1b6c39dc4aed3c93140cda8 to your computer and use it in GitHub Desktop.
Save PJZ9n/e116bb05d1b6c39dc4aed3c93140cda8 to your computer and use it in GitHub Desktop.
Webサーバー側
<?php
//とても単純な例で、そのまま使うことを前提としていません
//同一ディレクトリに所持金記録用のmoney.jsonがあるとする
function response(int $code, array $data = []): void{
http_response_code($code);
echo json_encode($data);
exit();
}
function setMoney(string $name, int $money): void{
$moneys = json_decode(file_get_contents("money.json"), true);
$moneys[$name] = $money;
file_put_contents("money.json", json_encode($moneys));
}
//アクセス元IPアドレスをチェック
if ($_SERVER["REMOTE_ADDR"] !== "123.456.789.012") {
//アクセス元がサーバーではなかったら401を返して終了
response(401);
}
setMoney($_POST["name"], $_POST["money"]);
response(200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment