172 lines
5.1 KiB
PHP
172 lines
5.1 KiB
PHP
<?php
|
|
|
|
// 개발 단계: 에러 표시
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// 세션 시작
|
|
session_start();
|
|
|
|
include '../config/dbconfig.php'; // 상대경로
|
|
include '../config/member.php';
|
|
|
|
$mem = new Member($db);
|
|
|
|
// ===== 공통 입력값 수집 =====
|
|
$id = isset($_POST['id']) ? trim($_POST['id']) : '';
|
|
$password = isset($_POST['password']) ? $_POST['password'] : '';
|
|
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
|
|
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
|
|
$zipcode = isset($_POST['zipcode']) ? trim($_POST['zipcode']) : '';
|
|
$addr1 = isset($_POST['addr1']) ? trim($_POST['addr1']) : '';
|
|
$addr2 = isset($_POST['addr2']) ? trim($_POST['addr2']) : '';
|
|
$mode = isset($_POST['mode']) ? trim($_POST['mode']) : '';
|
|
|
|
// ===== 비밀번호 규칙 서버 검증 함수 =====
|
|
// 최소 8자, 소문자/대문자/숫자/특수문자 각 1개 이상
|
|
function password_check($pwd)
|
|
{
|
|
return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$/', $pwd);
|
|
}
|
|
|
|
// ===== redirect + session 헬퍼 =====
|
|
function redirect_with_error($message)
|
|
{
|
|
// 이전 입력값 저장
|
|
$_SESSION['join_old'] = $_POST;
|
|
$_SESSION['join_error'] = $message;
|
|
|
|
// 폼 페이지로 리다이렉트 (member_input.php)
|
|
header('Location: ../member_input.php');
|
|
exit;
|
|
}
|
|
|
|
// ======================
|
|
// 1. 아이디 중복 체크 (AJAX)
|
|
// ======================
|
|
if ($mode === 'id_chk') {
|
|
|
|
if ($id === '') {
|
|
die(json_encode(['result' => 'empty_id']));
|
|
}
|
|
|
|
if ($mem->id_exists($id)) {
|
|
die(json_encode(['result' => 'fail']));
|
|
} else {
|
|
die(json_encode(['result' => 'success']));
|
|
}
|
|
|
|
// ======================
|
|
// 2. 이메일 중복 체크 (AJAX)
|
|
// ======================
|
|
} else if ($mode === 'email_chk') {
|
|
|
|
if ($email === '') {
|
|
die(json_encode(['result' => 'empty_email']));
|
|
}
|
|
|
|
// 이메일 형식 체크
|
|
if ($mem->email_format_check($email) === false) {
|
|
die(json_encode(['result' => 'email_format_wrong']));
|
|
}
|
|
|
|
// 이메일 중복 체크
|
|
if ($mem->email_exists($email)) {
|
|
die(json_encode(['result' => 'fail']));
|
|
} else {
|
|
die(json_encode(['result' => 'success']));
|
|
}
|
|
|
|
// ======================
|
|
// 3. 실제 회원 가입 처리
|
|
// ======================
|
|
} else if ($mode === 'input') {
|
|
|
|
// 1) 필수값 서버 검증 (JS만 믿지 않기)
|
|
if ($id === '' || $password === '' || $email === '') {
|
|
redirect_with_error('아이디, 비밀번호, 이메일은 필수 입력 항목입니다.');
|
|
}
|
|
|
|
// 2) 비밀번호 규칙 서버 검증
|
|
if (!password_check($password)) {
|
|
redirect_with_error('비밀번호는 8자 이상이며, 대문자/소문자/숫자/특수문자를 모두 포함해야 합니다.');
|
|
}
|
|
|
|
// 3) 선택 항목 기본값 처리 (미입력 시 빈 문자열)
|
|
$name = $name ?? '';
|
|
$zipcode = $zipcode ?? '';
|
|
$addr1 = $addr1 ?? '';
|
|
$addr2 = $addr2 ?? '';
|
|
|
|
// 4) 비밀번호 해시 처리
|
|
$hash = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
// 5) 프로필 이미지 처리 (선택 항목)
|
|
$photo = ''; // 기본값: 빈 문자열 또는 'default.jpg' 등으로 변경 가능
|
|
|
|
if (
|
|
isset($_FILES['photo']) &&
|
|
$_FILES['photo']['error'] === UPLOAD_ERR_OK &&
|
|
is_uploaded_file($_FILES['photo']['tmp_name'])
|
|
) {
|
|
$tmparr = explode('.', $_FILES['photo']['name']);
|
|
$ext = strtolower(end($tmparr)); // 확장자 소문자로 정리
|
|
|
|
// 허용 확장자
|
|
$allow_ext = ['jpg', 'jpeg', 'png', 'gif'];
|
|
if (!in_array($ext, $allow_ext)) {
|
|
redirect_with_error('이미지 파일(jpg, jpeg, png, gif)만 업로드 가능합니다.');
|
|
}
|
|
|
|
// 저장 파일명: 아이디.확장자
|
|
$photo = $id . '.' . $ext;
|
|
|
|
// 저장 경로
|
|
$target_dir = "../data/profile/";
|
|
$target_path = $target_dir . $photo;
|
|
|
|
// 디렉터리가 없으면 생성
|
|
if (!is_dir($target_dir)) {
|
|
mkdir($target_dir, 0777, true);
|
|
}
|
|
|
|
// 파일 복사
|
|
if (!copy($_FILES['photo']['tmp_name'], $target_path)) {
|
|
// 복사 실패 시 기본값 유지
|
|
$photo = '';
|
|
}
|
|
}
|
|
|
|
// 6) DB insert에 넘길 배열 생성
|
|
$arr = [
|
|
'id' => $id,
|
|
'password' => $hash,
|
|
'name' => $name,
|
|
'email' => $email,
|
|
'zipcode' => $zipcode,
|
|
'addr1' => $addr1,
|
|
'addr2' => $addr2,
|
|
'photo' => $photo
|
|
];
|
|
|
|
// 7) 회원 정보 저장
|
|
$mem->input($arr);
|
|
|
|
// 8) 가입 성공 시, 이전 입력/에러 세션 정리
|
|
unset($_SESSION['join_old'], $_SESSION['join_error']);
|
|
|
|
// 9) 회원가입 완료 후 알림 + 홈(index.php) 이동
|
|
echo "<script>
|
|
alert('회원가입이 완료되었습니다.');
|
|
window.location.href = '/member/index.php';
|
|
</script>";
|
|
exit;
|
|
}
|
|
|
|
// 기타 잘못된 접근에 대한 처리 (옵션)
|
|
// else {
|
|
// redirect_with_error('잘못된 접근입니다.');
|
|
// }
|
|
|