<목적>
: DB를 연동한 회원가입 및 로그인 페이지를 만들어라!
<목차>
- 페이지 소개
- 코드 분석
1. <페이지 소개>
드디어 전과 다르게 Sign up 이라는 항목이 생겻다!
이것을 누르게 되면!
이러한 회원가입 페이지로 들어오게 된다!
여기서 예외처리한 경우가 두경우가 있다.
1. 아이디가 중복이 되면 중복이라고 알려주기
2. 적지 않은 정보가 있으면 적으라고 알려주기
ID 가 중복일때 | 미입력 정보가 있을때 |
![]() |
![]() |
<회원가입이 완료되었을 때>
이렇게 DB와 연동하여 회원가입을 만들어 봤습니다.
이제 코드를 설명 해드리겠습니다.
2. <코드 분석>
<regist.php>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="div.css" rel="stylesheet" type = "text/css">
<title></title>
</head>
<body style="background-color:rgb(223, 209, 134);">
<div class ="full">
<p style="text-align:top; font-size:22px;"><a href ="index.php">Back</a>
<div class="sign-box">
<p style="text-align:center;font-family:Inter ">Sign up</p>
<form action="process.php" method="POST">
<input type="hidden" name="regist_req" value="request">
<p style="text-align:center"><input type="text" name ="reg_name" placeholder = "Enter your name " maxlength='20' class="id-form" ></p>
<p style="text-align:center"><input type="text" name ="reg_id" placeholder = "Enter your ID " maxlength='30' class="id-form" ></p>
<p style="text-align:center"><input type="password" name ="reg_password" placeholder = "Enter your PassWord" maxlength='30' class="password-form"></p>
<p style="text-align:center"><input type="text" name ="reg_email" placeholder = "Enter your E-mail " maxlength='50' class="id-form" ></p>
<p style="text-align:center"><input type = submit class="submit-btn" value="Sign up"></p>
</div>
</div>
</body>
</html>
작성해야할 4가지의 내용중 변수를
1.name = reg_name
2.id = reg_id
3.password = reg_password
4.email = reg_email
5. 마지막으로 hidden 으로 전달하는 regist_req 변수!
라고 설정후 form 태그로 process.php 에다가 전달을 해주엇습니다.
<process.php>
<?php
include 'dbcon.php';
if($_GET['score_name']){ //점수를 출력해주는 함수
$user_name = $_GET['score_name'];
$sql="select * from test where name='".$user_name."'";
$result = mysqli_query($dbcon,$sql);
$row = mysqli_fetch_array($result);
if($row['name']){
mysqli_free_result($result);
$sql = "select score from test where name='".$user_name."'";
$result = mysqli_query($dbcon,$sql);
$score=mysqli_fetch_array($result);
echo "<script>alert('{$user_name} 학생의 점수는 {$row['score']} 입니다');
location.href='/page.php'
</script>";
}
else{
echo "<script>alert('존재하지 않는 학생명 입니다.');
location.href='/page.php'
</script>";
}
}
if($_POST['regist_req']){ //회원가입 요청처리 함수
$user_name = $_POST['reg_name'];
$user_id = $_POST['reg_id'];
$user_password = $_POST['reg_password'];
$user_email=$_POST['reg_email'];
$sql="select id from test where id='".$user_id."'";
$result = mysqli_query($dbcon,$sql);
$row = mysqli_fetch_array($result);
if(!(($user_name)&&($user_id)&&($user_password)&&($user_email))){
echo "<script>alert('입력하지 않은 정보가 있습니다.');
location.href='/regist.php'
</script>";
}
else if($row){
echo "<script>alert('이미 존재하는 ID 입니다');
location.href='/regist.php'
</script>";
}else{
$sql = "insert into test (name,id,password,email) values ('".$user_name."','".$user_id."','".$user_password."','".$user_email."')";
mysqli_query($dbcon,$sql);
echo "<script>alert('회원가입이 완료되었습니다!');
location.href='/index.php'
</script>";
}
mysqli_free_result($result);
}
if($_POST['login_req']){ //로그인 요청처리 함수
$user_id = $_POST['id'];
$user_password = $_POST['password'];
$sql = "select * from test where id='".$user_id."'";
$result = mysqli_query($dbcon,$sql);
if(empty($user_id)||empty("$user_password")){ //id 나 password 를 입력 안햇을때
echo "<script>alert('ID 혹은 비밀번호를 입력해주세요.');
location.href='/index.php'
</script>"; //알림창이 뜨고 다시 index.php로 리다이렉션 하는 코드
}
else if($result){ //입력한 id 가 db에 있을때 인증절차 시작
$row=mysqli_fetch_array($result);
if($row['password']==$user_password && $row['id']==$user_id){
header('Location: /page.php'); //리다이렉션 하는 코드
}
else{
echo "<script>alert('ID 혹은 비밀번호가 틀렸습니다.');
location.href='/index.php'
</script>"; //알림창 뜨고 다시 index.php로 리다이렉션 하는코드
}
}
}
?>
점점 기능이 많아지다보니 process 파일이 많아져서 하나의 파일에 기능코드를 몰아넣기 시작했습니다.
regist_req 변수가 들어오면 두번째 if 문이 동작을 하게 됩니다.
<< 안에서 등록절차가 if -> else if -> else 순으로 진행이 되게 됩니다 >>
if )1. 입력하지 않은 정보가 있는지 확인
else if ) 2. 입력한 ID가 중복인지 확인(DB를 통해 확인)
else ) 3.회원가입 절차 시작!
이렇게 마무리가 되면 회원가입이 완료됩니다!
* 이후 개선할 점은 로그인을 하여 들어갔을때 로그인 한 사람의 페이지가 유지가 되도록 만들어야 할것같습니다.
'웹개발(PHP-Mysql)' 카테고리의 다른 글
[웹 개발] 마이페이지(비밀번호 변경 & 개인정보 수정) (0) | 2024.07.16 |
---|---|
[웹 개발] 검색 및 정렬 기능 구현 (0) | 2024.07.16 |
[웹 개발] CRUD(생성,읽기,수정,삭제) 페이지 만들기 (0) | 2024.07.15 |
<2주차 과제> mini mission (0) | 2024.05.15 |
<1주차 과제> 웹 사이트 만들기 - 로그인 페이지 디자인(CSS) (1) | 2024.05.05 |