phpでログイン機能(2)

前回はログインの会員登録ですが今回はログイン・ログアウト機能の追加です。

ログインページは

<?php
header('X-FRAME-OPTIONS: SAMEORIGIN');
session_start();
$error = null;
include_once('./config.php');
if(isset($_POST['mail'], $_POST['passwd'])){
    $mail = $_POST['mail'];
    $passwd = $_POST['passwd'];
    $domain = $_SERVER['SERVER_NAME'];
    $pdo = new PDO('mysql:dbname=' . $databasename . ';host=' . $host . ';' , $user, $password);
    $sql = "SELECT passwd FROM user WHERE mail = :email;";
    $stmt = $pdo->prepare($sql);
    $stmt->bindValue(':email', $mail);
    $stmt->execute();
    $result = $stmt->fetch();
    if(password_verify($passwd, $result['passwd'])){
        $sql = "SELECT id FROM user WHERE mail = :email;";
        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':email', $mail);
        $stmt->execute();
        $id = $stmt->fetch(PDO::FETCH_COLUMN);
        $_SESSION['ID'] = $id;
        if(isset($_POST['keep'])){
            $time = time() + 15552000;
            setcookie('mail', $mail, $time, $domain);
        }
        header('Location: ./');
        exit;
    }else{
        $error = 'ログインできませんでした';
    }
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ログイン</title>
</head>
<body>
    <?php
    echo $error;
    ?>
    <p>ログインページ</p>
    <form method="POST">
        <?php
        if(isset($_COOKIE['mail'])){
        ?>
        <div>
            <label>
                <input type="email" name="mail" value=<?php echo $_COOKIE['mail'] ?> required>メールアドレス
            </label>
        </div>
        <?php
        }else{
        ?>
        <div>
            <label>
                <input type="email" name="mail" required>メールアドレス
            </label>
        </div>
        <?php
        }
        ?>
        <div>
            <label>
                <input type="password" name="passwd" required>パスワード
            </label>
        </div>
        <div>
            <label>
                <input type="checkbox" name="keep">メールアドレスをcookieに保存する
            </label>
        </div>
        <input type="submit" value="ログインする">
    </forn>
</body>
</html>

「メールアドレスをcookieに保存する」はiPhoneでは使えませんでした。

使うとしたらセッションに保存することです。

ログアウトページ

<?php
session_start();
include_once('./config.php');
if(isset($_SESSION['ID'])){
    unset($_SESSION['ID']);
    session_destroy();
    header('Location: ./');
    exit;
}else{
    http_response_code(400);
}

セッションにあるIDを消すだけです。

セッションにIDがない場合は400エラーを返します。

indexページ

<?php
if(isset($_SESSION['ID'])){
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ログイン</title>
</head>
<body>
    <p>ログインできています。</p>
</body>
</html>
<?php
}else{
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ログイン</title>
</head>
<body>
    <p>ログアウトされています。</p>
</body>
</html>
<?php
}

$_SESSION[‘ID’]がある場合はログインできているようにしています。

$_SESSION[‘ID’]がない場合はログアウト状態にしています。

この記事が良かったら、シェアしてね!

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

ABOUT US
いちる
スプラトゥーン2とゼルダの伝説ブレスオブザワイルドとMinecraftとフォートナイトをプレイしています。