Skip to content

Instantly share code, notes, and snippets.

@ichsanputr
Created June 27, 2025 12:21
Show Gist options
  • Select an option

  • Save ichsanputr/5ae0b9f068f88fa734e00bdac66a5371 to your computer and use it in GitHub Desktop.

Select an option

Save ichsanputr/5ae0b9f068f88fa734e00bdac66a5371 to your computer and use it in GitHub Desktop.
<?php
// 방명록 데이터 읽기
$방명록파일 = "guestbook.txt";
$메시지들 = array();
if (file_exists($방명록파일)) {
$내용 = file_get_contents($방명록파일);
$메시지들 = json_decode($내용, true) ?: array();
}
// 새 메시지 저장
if ($_POST && !empty($_POST['name']) && !empty($_POST['message'])) {
$새메시지 = array(
'name' => htmlspecialchars($_POST['name']),
'message' => htmlspecialchars($_POST['message']),
'time' => date('Y-m-d H:i:s')
);
$메시지들[] = $새메시지;
file_put_contents($방명록파일, json_encode($메시지들));
header("Location: " . $_SERVER['PHP_SELF']); // 새로고침 방지
exit;
}
?>
<h1>방명록</h1>
<form method="POST">
이름: <input type="text" name="name" required><br>
메시지: <textarea name="message" required></textarea><br>
<button type="submit">작성하기</button>
</form>
<h2>방명록 내용</h2>
<?php foreach (array_reverse($메시지들) as $메시지): ?>
<div style="border: 1px solid #ccc; margin: 10px; padding: 10px;">
<strong><?php echo $메시지['name']; ?></strong>
(<?php echo $메시지['time']; ?>)<br>
<?php echo nl2br($메시지['message']); ?>
</div>
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment