<PHP> 로그인/로그아웃

아토밍·2024년 7월 10일

목차

login_view.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>로그인</title>
    <script src="login.js"></script>
</head>
<body>
    <h1>로그인</h1>
    <form method="post" action="login_ok.php" name="login_form">
        <label for="">아이디</label>
        <input type="text" name="id" id="id" placeholder="아이디 입력해주세요">
        <label for="">패스워드</label>
        <input type="Password" name="pw" id="pw" placeholder="비번 입력해주세요">
        <br>
        <button id="login_btn">확인<button>
    </form>

    <a href="member.php">회원전용 페이지</a>
</body>
</html>

login_ok.php

<?php
$id = (isset($_POST['id']) && $_POST['id'] != '') ? $_POST['id'] : '';
$pw = (isset($_POST['pw']) && $_POST['pw'] != '') ? $_POST['pw'] : '';

if($id == '') {
    echo "<script>
    alert('아이디를 입력 바랍니다')
    histroy.go(-1)
    </script>";
}

if($pw == '') {
    echo "<script>
    alert('비번을 입력 바랍니다')
    histroy.go(-1)
    </script>
    ";
}

if($id == 'guest' && $pw == '0000'){
    session_start();

    $_SESSION['ss_id'] = $id;

    echo "<script>
    alert('로그인 성공했습니다.');
    self.location.href='member.php'; //회원전용페이지 이동
    </script>";

    }else {
    echo "<script>
    alert('로그인 실패하였습니다. 아이디와 비번을 입력해주세요.');
    self.location.href='login_view.php';
    </script>";
}

?>

member.php

<?php
session_start();

if( !isset($_SESSION['ss_id']) or $_SESSION['ss_id'] == '' ){
    echo "
        <script>
        alert('여기는 회원 전용 입니다');
        self.location.href='login_view.php';
        </script>
        ";
    exit;
}
?>

<p>여기는 회원 전용 페이지 입니다.</p>
<a href="logout.php">로그아웃</a>

logout.php

<?php
session_start();
session_unset();
session_destroy();
?>

<script>
    alert('로그아웃되었습니다');
    self.location.href='login_view.php';
</script>

login.js

document.addEventListener('DOMContentLoaded', () => {
    const id = document.querySelector('#id')
    const pw = document.querySelector('#pw')
    const btn = document.querySelector('#login_btn')
    btn.addEventListener('click', (e) => {
        e.preventDefault()
        if(id.value == '') {
            alert('아이디를 입력해주세요')
            id.focus()
            return false
        }

        if(pw.value == '') {
            alert('비번을 입력해주세요')
            pw.focus()
            return false
        }
        document.login_form.submit()
    })
}) 
profile
뚜벅쵸

0개의 댓글