본문 바로가기

Web/Develop_PHP+MySQL

웹 페이지 만들기 - 비밀번호 일치 체크하기

1. signup.html

<!DOCTYPE html>
<html lang="ko">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name='viewport' content='width=device-width, intial-scale=1.0'>

	<link rel="stylesheet" href="/css/account.css">
	<title>Alioth's 회원가입 페이지</title>

</head>
<body>
	<div class="account-form">
	<h2>Sign Up</h2>

	<form action="signup.php" method="post">
		<p><input type="text" name="name" placeholder="이름" required class="text-field"></p>
		<p><input type="text" name="userid" placeholder="아이디" required class="text-field"></p>
		<p><input type="password" name="userpw" placeholder="비밀번호" required class="text-field"></p>
		<!-- 비밀번호 일치 확인을 위한 input 블록 추가 -->
		<p><input type="password" name="pwcheck" placeholder="비밀번호 확인" required class="text-field"></p>
		<p><input type="text" name="email" placeholder="이메일"  class="text-field"></p>
		<p><input type="submit" value="회원가입" class="submit-btn"></p>
	</form>
	</div>
</body>
</html>

비밀번호 확인 칸이 생성된 모습

2. signup.php

<?php
// DB 연결 정보 가져오기
include("function/dbconn.php");

// input 태그에서 전달된 사용자 입력 값 가져오기
$userid = $_POST['userid'];
$userpw = $_POST['userpw'];
// pwcheck 값 가져오기
$pwcheck = $_POST['pwcheck'];
$name = $_POST['name'];
$email = $_POST['email'];

// 비밀번호가 일치하다면
if($userpw==$pwcheck) {
    // 중복된 id 체크 쿼리 작성
    $idcheck = $conn->prepare("SELECT userid FROM users WHERE userid=?");
    $idcheck->bind_param("s", $userid);
    $idcheck->execute();

    // 반환된 SELECT 결과 저장
    $checkresult = $idcheck->get_result();

    // 회원 정보 DB 추가 쿼리 작성
    $sql = $conn->prepare("INSERT INTO users(userid,userpw,name,email,created) VALUES(?, ?, ?, ?, NOW())");
    $sql->bind_param("ssss", $userid, $userpw, $name, $email);

    // 반환된 SELECT 결과의 개수 확인(num_rows)
    if ($checkresult->num_rows > 0) {
        echo "<script>alert('중복된 ID 입니다!!');</script>";
    ?>
        <script>
            location.replace('signup.html')
        </script>
    <?php
    } else {
        $sql->execute();
        // 쿼리로 영향을 받은 행 개수 확인(affected_rows)
        $result = $sql->affected_rows;
        if ($result > 0) {
            echo "<script>alert('회원가입 성공!!');</script>";
        } else {
            echo "<script>alert('회원가입 실패..');</script>";
        }
    }
// 비밀번호가 일치하지 않다면
} else {
	// 알림창 띄우기
    echo "<script>alert('비밀번호가 일치하지 않습니다.');</script>";
    ?>
    <script>
    	// 회원가입 폼으로 다시 돌아가기
        location.replace('signup.html')
    </script>
<?php
}

// 연결 닫기
$sql->close();
$idcheck->close();
$conn->close();
?>

<script>
    location.replace('index.php')
</script>

비밀번호 불일치 시, 알림창을 띄워주는 것을 확인할 수 있다.