MIXEDER For Developers
MixederCloud MIXEDERGrapesFilesLogin to console
MixederCloud Docs
MixederCloud Docs
  • 👋MIXEDER For Developersへようこそ!
  • まずはここから!
    • StartFromBeginner
  • 🍇GrapeAPI
    • GrapesAPIについて
    • GrapesAPIs 利用規約
    • APIリファレンス
      • GrapeQRAPI リファレンス
      • GrapeFaviconAPI リファレンス
      • GrapeAvaterAPI リファレンス
        • GrapesAPI サンプルコード
      • GrapesAPI(個人向け) リファレンス
    • Swagger
  • Conversion API
    • Conversion APIとは?
    • APIドキュメント
      • SVG Via PNG API
      • PNG Via HEIC API
      • HTML to PDF API
  • 🌍MixederPublicAPIs
    • MixederPublicAPIについて
    • サンプルコードを利用する前に
    • APIリファレンス
      • WhoisAPI
        • WhoisAPI TLDリスト
      • ConvertAPI
      • TimeZoneAPI
      • WEBFetchAPI
      • NETRecordAPI
      • WebInfoAPI
      • ImageConvertAPI
      • Profanity Detection and Cleaning API
      • Similarity Determination API
      • API利用料金
      • JSON Scalable Database (JSDB) API
        • ログイン・サインインシステムのサンプル
        • シンプルなメモ帳のサンプル
        • JSDB V1とV2の相違点
      • JSON Database API V2
        • JSONDB Login System (Node.js)
        • JSONDB管理アプリの紹介
      • EventFront MAIL API ドキュメント
  • 🐣TinytoolsAPI
    • TINYTOOLSAPIについて
    • TINYTOOLSAPI for Enterpriceについて(英語)
  • Product Guides
    • 📪How to configure MTM(Mixeder Traffic Manager) with MixederWaterStorage
      • VMC Machine specs per plan
    • MIXEDER ISSHとは?
    • 🖥️開発者向けAPIの公開について
    • 📎MixederIAM(Permissions)
  • MixederCloudのサービス料金・お支払サイクルについて
  • MixederCloudでウェブサイトを作成しよう
    • 🏋️MixederCloudでウェブサイトを作成しよう
GitBook提供
このページ内
  • ファイル構成
  • functions.php (共通関数)
  • register.php (サインアップページ)
  • login.php (ログインページ)
  • index.php (ホームページ)
  • logout.php (ログアウト処理)
  • style.css (簡易スタイルシート)
  • 説明
  • 注意点

役に立ちましたか?

  1. MixederPublicAPIs
  2. APIリファレンス
  3. JSON Scalable Database (JSDB) API

ログイン・サインインシステムのサンプル

このコードは少人数(1~10000)程向けのユーザーに対応したログインシステムを想定しています。

PHPとJSDB API を利用したシンプルなアカウント登録・ログインシステムの実装例です。 ユーザーのアカウント情報をJSDBに保存し、セッションを使ってログイン状態を管理します。


ファイル構成

/auth_system
├── index.php        // ホームページ(ログイン後に表示)
├── login.php        // ログインページ
├── register.php     // サインアップ(アカウント登録)ページ
├── logout.php       // ログアウト処理
├── style.css        // 簡易スタイルシート(オプション)
└── functions.php    // APIやセッション管理を行う共通関数

functions.php (共通関数)

<?php
session_start();

$apiKey = "";  // APIで使用するデータベース名
$apiEndpoint = "https://events-front.mixeder.net/v2/jsdb";

// JSDBからユーザーを取得する関数
function getUsers() {
    global $apiKey, $apiEndpoint;
    $response = file_get_contents("$apiEndpoint?key=$apiKey&action=read");
    return json_decode($response, true) ?? [];
}

// 新しいユーザーをJSDBに保存する関数
function registerUser($username, $password) {
    global $apiKey, $apiEndpoint;
    $hashedPassword = password_hash($password, PASSWORD_BCRYPT);  // パスワードのハッシュ化
    $newUser = json_encode(["username" => $username, "password" => $hashedPassword]);

    $response = file_get_contents("$apiEndpoint?key=$apiKey&action=add&data=" . urlencode($newUser));
    return json_decode($response, true);
}

// ユーザーが存在するかを確認する関数
function findUser($username) {
    $users = getUsers();
    foreach ($users as $user) {
        if ($user['username'] === $username) {
            return $user;
        }
    }
    return null;
}

// ログイン状態を確認する関数
function isLoggedIn() {
    return isset($_SESSION['username']);
}
?>

register.php (サインアップページ)

<?php
require 'functions.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (findUser($username)) {
        $error = "このユーザー名は既に使用されています。";
    } else {
        registerUser($username, $password);
        header('Location: login.php');
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>サインアップ</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>サインアップ</h1>
    <form method="POST">
        <input type="text" name="username" placeholder="ユーザー名" required>
        <input type="password" name="password" placeholder="パスワード" required>
        <button type="submit">登録</button>
    </form>
    <?php if (isset($error)) echo "<p>$error</p>"; ?>
    <a href="login.php">ログインはこちら</a>
</body>
</html>

login.php (ログインページ)

<?php
require 'functions.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $user = findUser($username);

    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['username'] = $username;
        header('Location: index.php');
        exit();
    } else {
        $error = "ユーザー名またはパスワードが間違っています。";
    }
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>ログイン</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>ログイン</h1>
    <form method="POST">
        <input type="text" name="username" placeholder="ユーザー名" required>
        <input type="password" name="password" placeholder="パスワード" required>
        <button type="submit">ログイン</button>
    </form>
    <?php if (isset($error)) echo "<p>$error</p>"; ?>
    <a href="register.php">アカウント登録はこちら</a>
</body>
</html>

index.php (ホームページ)

<?php
require 'functions.php';

if (!isLoggedIn()) {
    header('Location: login.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>ホーム</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>ようこそ、<?= htmlspecialchars($_SESSION['username']) ?>さん!</h1>
    <a href="logout.php">ログアウト</a>
</body>
</html>

logout.php (ログアウト処理)

<?php
session_start();
session_destroy();
header('Location: login.php');
exit();
?>

style.css (簡易スタイルシート)

body {
    font-family: 'Arial', sans-serif;
    background-color: #f0f0f0;
    text-align: center;
    margin-top: 50px;
}

input {
    margin: 10px;
    padding: 10px;
    width: 80%;
}

button {
    padding: 10px 20px;
    margin-top: 10px;
}

p {
    color: red;
}

a {
    display: block;
    margin-top: 20px;
}

説明

  1. アカウント登録 (register.php): ユーザー名とパスワードを登録し、JSDBに保存します。

  2. ログイン (login.php): 登録済みのユーザー名とパスワードでログインし、セッションを開始します。

  3. ホームページ (index.php): ログイン後、ユーザー名を表示するページです。

  4. ログアウト (logout.php): セッションを破棄してログアウトします。


注意点

  • パスワードのハッシュ化: password_hash() 関数でパスワードを安全に保存します。

  • JSDBのAPI: ユーザーデータをJSON形式で管理するため、DBサーバーは不要です。

  • セッション管理: PHPセッションでログイン状態を管理します。

前へJSON Scalable Database (JSDB) API次へシンプルなメモ帳のサンプル

最終更新 6 か月前

役に立ちましたか?

🌍
Page cover image