
PHP ๊ฐ์
>PHP - ์ค๊ธ
๐ PHP ์ค๊ธ - 2์ฃผ์ฐจ: ํ์๊ฐ์ ๋ฐ ๋ก๊ทธ์ธ ์์คํ ๊ตฌํ - 02 ํ์๊ฐ์ ๊ธฐ๋ฅ (๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ, ๋น๋ฐ๋ฒํธ ํด์ฑ)
![]() |
ํ์ | 10.0 | ๋ผ์ด์ผ์ค | free |
---|---|---|---|---|
์ฌ์ฉ์ํ์ | 10.0 | ์ด์์ฒด์ | ||
๋ค์ด๋ก๋ | 1 | ํ์ผํฌ๊ธฐ | 0 | |
์ ์์ฌ | LUZENSOFT | ๋ฑ๋ก์ผ | 2025-07-17 15:28:53 | |
์กฐํ์ | 8 |
๐ PHP ์ค๊ธ - 2์ฃผ์ฐจ: ํ์๊ฐ์ ๋ฐ ๋ก๊ทธ์ธ ์์คํ ๊ตฌํ - 02 ํ์๊ฐ์ ๊ธฐ๋ฅ (๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ, ๋น๋ฐ๋ฒํธ ํด์ฑ)
์ด๋ฒ ํฌ์คํ ์์๋ ์ง๋์ฃผ์ ์ด์ด #ํ์๊ฐ์ ๋ฐ #๋ก๊ทธ์ธ ์์คํ ๊ตฌํ์ ๋ ๋ฒ์งธ ๋จ๊ณ๋ก, #ํ์๊ฐ์ ๊ธฐ๋ฅ์ ์ด์ ์ ๋ง์ถฐ๋ณด๊ฒ ์ต๋๋ค. ํนํ, ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ #๋ฐ์ดํฐ์ #์ ํจ์ฑ์ ๊ฒ์ฌํ๊ณ #๋น๋ฐ๋ฒํธ๋ฅผ #์์ ํ๊ฒ #ํด์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์ธํ ์์๋ณด๊ฒ ์ต๋๋ค.
1. ํ์๊ฐ์ ํผ (HTML)
ํ์๊ฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํ๊ธฐ ์ํด์๋ ๋จผ์ ์ฌ์ฉ์๋ก๋ถํฐ ํ์ํ ์ ๋ณด๋ฅผ ์ ๋ ฅ๋ฐ์ #HTML #ํผ์ด ํ์ํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก #์์ด๋, #๋น๋ฐ๋ฒํธ, #๋น๋ฐ๋ฒํธ ํ์ธ, #์ด๋ฉ์ผ ๋ฑ์ ํ๋๊ฐ ํฌํจ๋ฉ๋๋ค.
HTML
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ํ์๊ฐ์
</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.container { max-width: 400px; margin: auto; padding: 20px; border: 1px solid #ccc; border-radius: 8px; }
h2 { text-align: center; }
label { display: block; margin-bottom: 8px; }
input[type="text"],
input[type="password"],
input[type="email"] {
width: calc(100% - 20px);
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.error { color: red; margin-bottom: 10px; }
</style>
</head>
<body>
<div class="container">
<h2>ํ์๊ฐ์
</h2>
<?php
session_start();
if (isset($_SESSION['error_message'])) {
echo '<div class="error">' . htmlspecialchars($_SESSION['error_message']) . '</div>';
unset($_SESSION['error_message']); // ๋ฉ์์ง ํ์ ํ ์ญ์
}
?>
<form action="register_process.php" method="POST">
<label for="username">์์ด๋:</label>
<input type="text" id="username" name="username" required>
<label for="password">๋น๋ฐ๋ฒํธ:</label>
<input type="password" id="password" name="password" required>
<label for="confirm_password">๋น๋ฐ๋ฒํธ ํ์ธ:</label>
<input type="password" id="confirm_password" name="confirm_password" required>
<label for="email">์ด๋ฉ์ผ:</label>
<input type="email" id="email" name="email" required>
<button type="submit">ํ์๊ฐ์
</button>
</form>
</div>
</body>
</html>
2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์ค์
ํ์ ์ ๋ณด๋ฅผ ์ ์ฅํ #๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐํ๋ ์ค์ ์ด ํ์ํฉ๋๋ค. db_config.php
ํ์ผ์ ํตํด #๋ฐ์ดํฐ๋ฒ ์ด์ค #์ฐ๊ฒฐ์ ๊ด๋ฆฌํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
PHP
<?php
// db_config.php
$db_host = 'localhost'; // ๋ฐ์ดํฐ๋ฒ ์ด์ค ํธ์คํธ
$db_user = 'root'; // ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ์๋ช
$db_pass = 'your_password'; // ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋น๋ฐ๋ฒํธ (์ค์ ์ฌ์ฉ ์ ๋ณ๊ฒฝ)
$db_name = 'your_database_name'; // ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ช
(์ค์ ์ฌ์ฉ ์ ๋ณ๊ฒฝ)
// MySQLi ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
// ์ฐ๊ฒฐ ์ค๋ฅ ํ์ธ
if ($conn->connect_error) {
die("๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์คํจ: " . $conn->connect_error);
}
// ๋ฌธ์ ์ธ์ฝ๋ฉ ์ค์ (UTF-8)
$conn->set_charset("utf8mb4");
?>
3. ํ์๊ฐ์ ์ฒ๋ฆฌ ๋ก์ง (PHP)
register_process.php
ํ์ผ์์ ์ค์ ํ์๊ฐ์
์ฒ๋ฆฌ ๋ก์ง์ ๊ตฌํํฉ๋๋ค. ์ด ๋ถ๋ถ์์๋ ๋ค์ ๋จ๊ณ๋ฅผ ๊ฑฐ์นฉ๋๋ค:
#๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ: ์ ๋ ฅ๋ ๋ฐ์ดํฐ๊ฐ #์ ํจํ์ง ํ์ธํฉ๋๋ค.
#๋น๋ฐ๋ฒํธ #ํด์ฑ: ๋ณด์์ ์ํด ๋น๋ฐ๋ฒํธ๋ฅผ ํด์ฑํฉ๋๋ค.
#๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ: ์ ํจํ๊ณ ํด์ฑ๋ ๋ฐ์ดํฐ๋ฅผ #๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํฉ๋๋ค.
PHP
<?php
// register_process.php
session_start(); // ์ธ์
์์
// ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์ ํ์ผ ํฌํจ
require_once 'db_config.php';
// POST ์์ฒญ์ด ์๋๋ฉด ๋ฆฌ๋ค์ด๋ ํธ
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: register.php');
exit();
}
// 1. ์
๋ ฅ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ ๋ฐ ๊ณต๋ฐฑ ์ ๊ฑฐ
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
$email = trim($_POST['email'] ?? '');
// 2. ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ
$errors = [];
// ์์ด๋ ์ ํจ์ฑ ๊ฒ์ฌ
if (empty($username)) {
$errors[] = "์์ด๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.";
} elseif (!preg_match('/^[a-zA-Z0-9]{4,20}$/', $username)) {
$errors[] = "์์ด๋๋ ์๋ฌธ, ์ซ์ ์กฐํฉ 4~20์๋ก ์
๋ ฅํด์ฃผ์ธ์.";
}
// ๋น๋ฐ๋ฒํธ ์ ํจ์ฑ ๊ฒ์ฌ
if (empty($password)) {
$errors[] = "๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.";
} elseif (strlen($password) < 8) {
$errors[] = "๋น๋ฐ๋ฒํธ๋ ์ต์ 8์ ์ด์์ด์ด์ผ ํฉ๋๋ค.";
} elseif ($password !== $confirm_password) {
$errors[] = "๋น๋ฐ๋ฒํธ์ ๋น๋ฐ๋ฒํธ ํ์ธ์ด ์ผ์นํ์ง ์์ต๋๋ค.";
}
// ์ด๋ฉ์ผ ์ ํจ์ฑ ๊ฒ์ฌ
if (empty($email)) {
$errors[] = "์ด๋ฉ์ผ์ ์
๋ ฅํด์ฃผ์ธ์.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "์ ํจํ์ง ์์ ์ด๋ฉ์ผ ํ์์
๋๋ค.";
}
// ์๋ฌ๊ฐ ๋ฐ์ํ๋ค๋ฉด ์ธ์
์ ์ ์ฅ ํ ํ์๊ฐ์
ํผ์ผ๋ก ๋ฆฌ๋ค์ด๋ ํธ
if (!empty($errors)) {
$_SESSION['error_message'] = implode('<br>', $errors);
header('Location: register.php');
exit();
}
// 3. ์์ด๋ ์ค๋ณต ํ์ธ
// SQL Injection ๋ฐฉ์ง๋ฅผ ์ํด prepared statement ์ฌ์ฉ
$stmt = $conn->prepare("SELECT id FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$_SESSION['error_message'] = "์ด๋ฏธ ์กด์ฌํ๋ ์์ด๋์
๋๋ค. ๋ค๋ฅธ ์์ด๋๋ฅผ ์ฌ์ฉํด์ฃผ์ธ์.";
$stmt->close();
$conn->close();
header('Location: register.php');
exit();
}
$stmt->close();
// 4. ๋น๋ฐ๋ฒํธ ํด์ฑ (๋งค์ฐ ์ค์!)
// password_hash() ํจ์๋ ์ํธ(salt)๋ฅผ ์๋์ผ๋ก ์์ฑํ์ฌ ์์ ํ๊ฒ ํด์ฑํฉ๋๋ค.
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
// 5. ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ
// SQL Injection ๋ฐฉ์ง๋ฅผ ์ํด prepared statement ์ฌ์ฉ
$stmt = $conn->prepare("INSERT INTO users (username, password, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $hashed_password, $email);
if ($stmt->execute()) {
// ํ์๊ฐ์
์ฑ๊ณต
$_SESSION['success_message'] = "ํ์๊ฐ์
์ด ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋์์ต๋๋ค.";
header('Location: login.php'); // ์ฑ๊ณต ์ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ์ด๋
exit();
} else {
// ํ์๊ฐ์
์คํจ
$_SESSION['error_message'] = "ํ์๊ฐ์
์ ์คํจํ์ต๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์. ์ค๋ฅ: " . $stmt->error;
header('Location: register.php');
exit();
}
// ์ฐ๊ฒฐ ์ข
๋ฃ
$stmt->close();
$conn->close();
?>
4. ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ ์์ธ ์ค๋ช
register_process.php
์์ ์ฌ์ฉ๋ ์ฃผ์ #๋ฐ์ดํฐ #์ ํจ์ฑ #๊ฒ์ฌ ๊ธฐ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
trim()
: ์ฌ์ฉ์ ์ ๋ ฅ์ ์๋ค ๊ณต๋ฐฑ์ ์ ๊ฑฐํ์ฌ ์ผ๊ด๋ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.empty()
: ํ์ ํ๋๊ฐ ๋น์ด ์๋์ง ํ์ธํฉ๋๋ค.preg_match()
: #์ ๊ทํํ์์ ์ฌ์ฉํ์ฌ ์์ด๋๊ฐ ํน์ ํจํด(์๋ฌธ, ์ซ์ 4~20์)์ ๋ฐ๋ฅด๋์ง ๊ฒ์ฌํฉ๋๋ค.strlen()
: ๋น๋ฐ๋ฒํธ์ ์ต์ ๊ธธ์ด๋ฅผ ๊ฒ์ฌํฉ๋๋ค.filter_var($email, FILTER_VALIDATE_EMAIL)
: #PHP ๋ด์ฅ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฉ์ผ ์ฃผ์์ ํ์์ด ์ ํจํ์ง ํ์ธํฉ๋๋ค.#๋น๋ฐ๋ฒํธ ์ผ์น ํ์ธ:
password === confirm_password
๋ฅผ ํตํด ๋ ๋น๋ฐ๋ฒํธ ์ ๋ ฅ์ด ์ผ์นํ๋์ง ํ์ธํฉ๋๋ค.#์์ด๋ ์ค๋ณต ํ์ธ: ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋์ผํ ์์ด๋๊ฐ ์ด๋ฏธ ์กด์ฌํ๋์ง ์ฟผ๋ฆฌ๋ฅผ ํตํด ํ์ธํฉ๋๋ค. #Prepared #Statement๋ฅผ ์ฌ์ฉํ์ฌ #SQL #Injection ๊ณต๊ฒฉ์ ๋ฐฉ์งํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
5. ๋น๋ฐ๋ฒํธ ํด์ฑ (Hashing)
#๋น๋ฐ๋ฒํธ๋ฅผ #ํ๋ฌธ์ผ๋ก #๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๋ ๊ฒ์ #๋งค์ฐ #์ํํฉ๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์ ์ถ๋ ๊ฒฝ์ฐ ๋ชจ๋ ์ฌ์ฉ์ ๋น๋ฐ๋ฒํธ๊ฐ ๋ ธ์ถ๋๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด #๋น๋ฐ๋ฒํธ #ํด์ฑ์ด ํ์์ ์ ๋๋ค.
#PHP์์๋ password_hash()
ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋น๋ฐ๋ฒํธ๋ฅผ ์์ ํ๊ฒ ํด์ฑํ ์ ์์ต๋๋ค.
PHP
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
password_hash()
: ์ฃผ์ด์ง ๋ฌธ์์ด์ ํด์ฑํฉ๋๋ค.PASSWORD_BCRYPT
:bcrypt
#์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ์ฌ ํด์ฑํฉ๋๋ค.bcrypt
๋ ํ์ฌ ๊ฐ์ฅ ๋๋ฆฌ ์ฌ์ฉ๋๊ณ #์์ ํ๋ค๊ณ ํ๊ฐ๋ฐ๋ ํด์ฑ ์๊ณ ๋ฆฌ์ฆ ์ค ํ๋์ ๋๋ค. ์ด ํจ์๋ #์๋์ผ๋ก #์ํธ(salt)๋ฅผ ์์ฑํ์ฌ ํด์ฑ ๊ฒฐ๊ณผ์ ํฌํจ์ํค๋ฏ๋ก, ๋์ผํ ๋น๋ฐ๋ฒํธ๋ผ๋ ๋งค๋ฒ ๋ค๋ฅธ ํด์ ๊ฐ์ ๊ฐ์ง๊ฒ ๋์ด #๋ ์ธ๋ณด์ฐ #ํ ์ด๋ธ ๊ณต๊ฒฉ ๋ฑ์ ๋ฐฉ์งํฉ๋๋ค.
๋์ค์ ๋ก๊ทธ์ธ ์์๋ password_verify()
ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋น๋ฐ๋ฒํธ์ ํด์ฑ๋ ๋น๋ฐ๋ฒํธ๋ฅผ ๋น๊ตํ๊ฒ ๋ฉ๋๋ค.
6. ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ ์์ฑ (SQL)
์ PHP ์ฝ๋๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ๋์ํ๋ ค๋ฉด your_database_name
๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด์ users
ํ
์ด๋ธ์ด ์์ฑ๋์ด ์์ด์ผ ํฉ๋๋ค. ๋ค์์ users
ํ
์ด๋ธ์ ์์ฑํ๋ #SQL ์ฟผ๋ฆฌ ์์์
๋๋ค.
SQL
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
id
: ๊ฐ ์ฌ์ฉ์๋ฅผ ๊ณ ์ ํ๊ฒ ์๋ณํ๋ #๊ธฐ๋ณธ #ํค์ ๋๋ค.username
: ์ฌ์ฉ์ ์์ด๋๋ฅผ ์ ์ฅํฉ๋๋ค.UNIQUE
์ ์ฝ ์กฐ๊ฑด์ ์ถ๊ฐํ์ฌ ์ค๋ณต๋ ์์ด๋๋ฅผ ๋ฐฉ์งํฉ๋๋ค.password
: #ํด์ฑ๋ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ์ฅํฉ๋๋ค.VARCHAR(255)
๋bcrypt
ํด์ ๊ฐ์ ์ ์ฅํ๊ธฐ์ ์ถฉ๋ถํ ๊ธธ์ด์ ๋๋ค.email
: ์ฌ์ฉ์ ์ด๋ฉ์ผ์ ์ ์ฅํฉ๋๋ค. ์ญ์UNIQUE
์ ์ฝ ์กฐ๊ฑด์ ์ ์ฉํ ์ ์์ต๋๋ค.created_at
: ์ฌ์ฉ์ ๊ณ์ ์ด ์์ฑ๋ ์๊ฐ์ ๊ธฐ๋กํฉ๋๋ค.
๊ฒฐ๋ก
์ด๋ฒ ํฌ์คํ ์์๋ #PHP๋ฅผ ์ด์ฉํ #ํ์๊ฐ์ ๊ธฐ๋ฅ ๊ตฌํ์ ํต์ฌ์ธ #๋ฐ์ดํฐ #์ ํจ์ฑ ๊ฒ์ฌ์ #๋น๋ฐ๋ฒํธ #ํด์ฑ์ ๋ํด ์ดํด๋ณด์์ต๋๋ค. ์์ ํ๊ณ ๊ฒฌ๊ณ ํ ํ์๊ฐ์ ์์คํ ์ ๊ตฌ์ถํ๊ธฐ ์ํด์๋ ์ด๋ฌํ #๋ณด์ ์ธก๋ฉด์ ์ฒ ์ ํ ๊ณ ๋ คํด์ผ ํฉ๋๋ค.