실습 - 회원가입
1) members 테이블 생성
no int primary key auto_increment
id varchar(16)
pw varchar(20)
date datetime
name varchar(20)
create table members(
no int primary key auto_increment,
id varchar(16) not null,
pw varchar(20) not null,
date datetime not null,
name varchar(20) not null
);
insert into members(id, pw, date, name) values('pink','1234','2022-05-30','민영');
insert into members(id, pw, date, name) values('green','1234','2022-05-30','민영');
2) member 폴더
-> join.php 회원가입
-> join_process.php 회원가입 처리
->login.php 로그인
->login_process.php 로그인 처리
3)
header.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="/php/book_teach/css/style.css">
</head>
<body>
<div id="wrap">
<header>
<h1>Books</h1>
<ul>
<li><a href="/php/book_teach/index.php">home</a></li>
<li>
<?php
if(isset($_SESSION['userId'])){
echo "<a href ='/php/book_teach/process/process_logout.php'>로그아웃</a>";
}else {
echo "<a href ='/php/book_teach/member/login.php'>로그인</a>";
}
?>
</li>
<li><a href="/php/book_teach/member/join.php">회원가입</a></li>
<li><a href="/php/book_teach/index.php">도서목록</a></li>
<li><a href="/php/book_teach/create.php">도서등록</a></li>
<li><a href="">도서검색</a></li>
</ul>
</header>
<div id="contents">
join.php
<?php include_once '../include/header.php' ?>
<div id="write_book" class="inner">
<h2>회원가입</h2>
<h3>회원정보를 입력하세요</h3>
<form action="../process/process_join.php" method="post">
<table>
<tr>
<td>아이디</td>
<td><input type="text" name="userId"></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="userPw"></td>
</tr>
<tr>
<td>비밀번호체크</td>
<td><input type="password" name="userPwCh"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">회원가입</button>
<button type="reset">취소</button>
</td>
</tr>
</table>
</form>
</div>
<?php include_once '../include/footer.php' ?>
login.php
<?php include_once '../include/header.php' ?>
<div id="write_book" class="inner">
<h2>로그인</h2>
<h3>아이디와 패스워드를 입력하세요</h3>
<form action="../process/process_login.php" method="post">
<table>
<tr>
<td>아이디</td>
<td><input type="text" name="userId"></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="userPw"></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">로그인</button>
<button type="reset">취소</button>
<button type="button" onclick="location.href='join.php'">회원가입</button>
</td>
</tr>
</table>
</form>
</div>
<?php include_once '../include/footer.php' ?>
process_join.php
<?php
$conn = mysqli_connect('localhost','root','0115','test');
$query = "insert into members(id, pw, date, name)
values('{$_POST['userId']}','{$_POST['userPw']}',NOW(),'{$_POST['userName']}')";
$result = mysqli_query($conn, $query);
if($result){
echo "성공";
}else {
echo "실패";
}
header('Location:../index.php');
?>
process_login.php
<?php
session_start();
//members 테이블에 등록된 회원인지 확인
$conn = mysqli_connect('localhost','root','0115','test');
$query = "select * from members where id='{$_POST['userId']}';";
$result = mysqli_query($conn, $query);
//아이디가 있다면 비밀번호 검사
if(mysqli_num_rows($result)==1){
$row = mysqli_fetch_array($result);
//비밀번호 확인 -> 비밀번호가 맞으면 세션 생성
if($_POST['userPw']==$row['pw']){
$_SESSION['userId'] = $_POST['userId'];
//세션 아디가 있으면 로그인되었습니다. 경고창 출력
if(isset($_SESSION['userId'])){
?>
<script>
alert("로그인되었습니다.");
location.replace("../index.php");
</script>
<?php
}
}else{
?>
<script>
alert("비밀번호가 맞지 않습니다.");
location.replace("../index.php");
</script>
<?php
}
}else {
?>
<script>
alert("아이디가 맞지 않습니다.");
location.replace("../index.php");
</script>
<?php
}
?>
process_logout.php
<?php
session_start();
$result = session_destroy();
if($result){
?>
<script>
alert("로그아웃되었습니다.");
// location.replace('../index.php');
// 이전 페이지로 이동
history.back();
</script>
<?php
}
?>
'노력이 좋아서' 카테고리의 다른 글
<step52>'php_이미지 전송' (0) | 2022.06.02 |
---|---|
<step51>'과제 실습' (2) | 2022.05.31 |
<step50>'php_쿠키' (0) | 2022.05.30 |
<step50>'php_세션' (0) | 2022.05.30 |
<step50>'도서목록 페이징' (0) | 2022.05.30 |