회원가입 페이지를 PHP로 구현해 보았다.
역시 이쁘지 않다.

mysqli_real_escape_string() 이란?
php에서 제공하는 함수로 MYSQL과 커넥션을할때 String을 Escape한 상태로 만들어준다.
사용법 :
mysqli_real_escape_string(connection, escapestring);
- MYSQL 과 연결하는 connection과 escape형태로 만들어줄 string을 입력한다.
Escape string이란?
우리가 string을 입력할때 Tom's cat 이란 입력을 하면 '는 sql문에 앞서 있던 ' 와 중첩이 될 수 있다.
이러한 문제를 막기위해 \n, \r \" 처럼 구별해주는 형태로 만들어주는 것을 Escape string 이라고 한다.
1_register_view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<body>
<h1>회원가입</h1>
<hr><br>
<form name="register" action="1_register_server.php" method = "POST" autocomplete ="off">
<label>아이디 : </label>
<input type="text" id= "uid" name="id" placeholder = "ID">
<input type="hidden" name ="decide_id" id="decide_id">
<input type="button" id="check_button" value="ID 중복 검사" onclick="checkid();">
<span id="decide" style='color:red;'>ID 중복 여부를 확인해 주세요.</span>
<br>
<label>패스_1 : </label>
<input name="pass1" type="password" placeholder = "패스워드">
<br>
<label>패스_2 : </label>
<input name="pass2" type="password" placeholder = "패스워드 확인">
<br>
<label>이 름 : </label>
<input name="name" type="text" placeholder = "name">
<br>
<label>이메일 : </label>
<input name="email" type="email" placeholder = "email">
<br>
<label>주 소 : </label>
<input type="text" size = 40 placeholder ="주소를 입력하세요." id = "user_address" name ="user_address" onclick="address();">
<br><br>
<button type = "submit" id = "join_button" name = "confim" >가입</button>
<input type="button", name ="cancel" value ="취소" onclick ="location.href ='1_login_view.php'">
</form>
</body>
|
cs |
1_register_server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
<?php
include("db.php");
// $_POST방식으로 받아온 데이터 변수에 저장
$id = $_POST['decide_id'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['user_address'];
//시큐어 코딩
$id = mysqli_real_escape_string($conn,$id);
$pass1 = mysqli_real_escape_string($conn,$pass1);
$pass2 = mysqli_real_escape_string($conn,$pass2);
$name = mysqli_real_escape_string($conn,$name);
$email = mysqli_real_escape_string($conn,$email);
$address = mysqli_real_escape_string($conn,$address);
$pass1 = hash("sha256",$pass1); //sha256방식으로 해쉬화
$pass2 = hash("sha256",$pass2); //sha256방식으로 해쉬화
// 오류 체크
// 아이디나 패스워드 등등 입력이 안된 곳이 없게.
if(empty($id)){
echo "<script>alert('아이디를 입력해 주세요.')</script>";
echo "<script>location.replace('./1_register_view.php');</script>";
exit();
} else if(empty($email)){
echo "<script>alert('이메일을 입력해 주세요..')</script>";
echo "<script>location.replace('./1_register_view.php');</script>";
exit();
} else if(empty($pass1)){
echo "<script>alert('패스워드를 입력해 주세요.')</script>";
echo "<script>location.replace('./1_register_view.php');</script>";
exit();
} else if(empty($pass2)){
echo "<script>alert('패스워드를 확인해 주세요.')</script>";
echo "<script>location.replace('./1_register_view.php');</script>";
exit();
} else if($pass1 != $pass2){
echo "<script>alert('입력하신 패스워드가 일치하지 않습니다.')</script>";
echo "<script>location.replace('./1_register_view.php');</script>";
exit();
} else {
$sql_same ="SELECT * from basic_table where db_id ='$id' and db_email ='$email'";
$result_same = mysqli_query($conn, $sql_same);
if(mysqli_num_rows($result_same) > 0){ //회원가입시 입력한 아이디와 이메일이 데이터베이스에 이미 존재하는 경우 에러
echo "<script>alert('아이디 또는 이메일이 이미 존재합니다.')</script>";
echo "<script>location.replace('./1_register_view.php');</script>";
exit();
} else { //회원가입시 입력한 아이디와 이메일이 데이터베이스에 존재하지 않는다면 INSERT INTO로 데이터 삽입해주면 된다.
$sql_save = "insert into basic_table(db_id,db_pw,db_name,db_email,db_address) values ('$id','$pass1','$name','$email','$address')";
$result_save = mysqli_query($conn, $sql_save);
if($result_save){
echo "<script>alert('성공적으로 가입되었습니다, 로그인을 해 주세요.')</script>";
echo "<script>location.replace('./1_login_view.php');</script>";
exit();
} else {
echo "<script>alert('죄송합니다. 다시 회원가입을 진행해 주세요.')</script>";
echo "<script>location.replace('./1_register_view.php');</script>";
exit();
}
}
}
// 입력한 두개의 패스워드가 서로 같은 지 확인해야 하고,
// 동일한 아이디와 이메일이 있는지 확인 해야하고
// 그러면 회원가입 데이터베이스에 저장.
?>
|
cs |
'웹개발 > PHP- 홈페이지 구현' 카테고리의 다른 글
로그인 페이지_prepared statement (0) | 2023.01.02 |
---|---|
로그인 페이지 구현 (0) | 2023.01.01 |
데이터 전송 방식 GET & POST (0) | 2022.11.05 |
4일차_초보자를 위한 PHP 200제 (0) | 2022.10.25 |
3일차_초보자를 위한 PHP 200제 (0) | 2022.10.24 |