Skip to content
3 changes: 3 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RewriteEngine On

RewriteRule ^([^.]+)$ index.php?param=$1 [QSA,L]
42 changes: 42 additions & 0 deletions actions/user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

switch ($_POST['action']) {

case 'login' :
[
'userid' => $userid,
'password' => $pass,
] = $_POST;

$user = fetch("SELECT * FROM users WHERE id = ? AND password = password(?)", [$userid, $pass]);

access($user, "아이디 또는 비밀번호가 잘못되었습니다.");

$_SESSION['user'] = $user;
alert('로그인 되었습니다.');
move('/');

break;

case 'join' :
[
'userid' => $userid,
'password' => $pass,
'password_chk' => $passchk,
'username' => $username
] = $_POST;

access($userid !== "" && $pass !== "" && $username !== "", "빈칸이 존재합니다.");
access($pass == $passchk, "비밀번호 재입력의 값이 잘못되었습니다.");

$sql = "INSERT INTO users(`id`, `name`, `password`) VALUES (?, ?, PASSWORD(?))";

access(execute($sql, [$userid, $username, $pass]), "DB 에러");

alert("회원가입 완료.");
move("./login");

break;

default: break;
}
14 changes: 14 additions & 0 deletions actions/userSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

echo $_POST["search"];

switch($_POST['search']){
case "search":
[
'searchbox' => $searchbox,
'searchbtn' => $searchbtn,
] = $_POST;
Comment on lines +7 to +10

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$_POST에 넘어온 값을 풀어서 변수에 담는걸로 보이는데 맞나요?
맞다면 자바스크립트의 구조분해할당과 같은 역할을 하는 건가요?


$user = fetchAll("INSERT INTO users(`id`, `name`) VALUES(?,?)", [$searchbox]);

}
3 changes: 3 additions & 0 deletions api/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
$result = fetchAll("SELECT `id`, `name` FROM users WHERE id LIKE ?", ["%{$_GET['q']}%"]);
echo json_encode($result);
16 changes: 16 additions & 0 deletions core/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
$param = isset($_GET['param']) ? explode("/", $_GET['param']) : [];
$page = $param[0] ?? NULL;
$action = $param[1] ?? NULL;
$idx = $param[2] ?? NULL;
$include_file = $action ?? $page;
Comment on lines +2 to +6

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract함수를 사용해 보는 것은 어떨까요?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

참고해보도록 하겠습니당


if (isset($_POST['action'])) {
include_once(HOME . "/actions/{$page}.php");
exit;
}

if ($page === 'api') {
include_once(HOME . "/api/{$action}.php");
exit;
}
41 changes: 41 additions & 0 deletions core/lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

function alert($msg)
{
echo "<script>alert('{$msg}');</script>";
}

function move($url = false)
{
echo "<script>";
echo $url ? "location.replace('{$url}');" : "history.back()";
echo "</script>";
exit;
}

function access($bool, $msg, $url = false)
{
if (!$bool) {
alert($msg);
move($url);
}
}

function execute($sql, $data = [])
{
$db = new PDO("mysql:host=localhost; dbname=member; charset=utf8mb4;", "root", "");
$result = $db->prepare($sql);
$result->execute($data);
$db = null;
return $result;
}

function fetch($sql, $data = [])
{
return execute($sql, $data)->fetch(PDO::FETCH_OBJ);
}

function fetchAll($sql, $data = [])
{
return execute($sql, $data)->fetchAll(PDO::FETCH_OBJ);
}
16 changes: 16 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
session_start();

define("HOME", __dir__);

include_once(HOME . "/core/lib.php");
include_once(HOME . "/core/common.php");


include_once(HOME . "/view/template/header.php");
if (isset($include_file)) {
include_once(HOME . "/view/{$page}/{$include_file}.php");
} else {
include_once(HOME . "/view/template/main.php");
}
include_once(HOME . "/view/template/footer.php");
20 changes: 20 additions & 0 deletions js/captcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const code = [
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','r','j','k','l','m','n','o','p','q', 'r','s',
't', 'u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'
];

const getCaptchaString = () => Array(6)
.fill(0)
.map(() => Math.floor(Math.random() * code.length))
.map(index => code[index])
.join('');

document.querySelector('#maincaptcha').value = getCaptchaString();

if(document.getElementById("maincaptcha").length < 7){
document.getElementById("maincaptcha").empty();
}
// document.oncontextmenu = function() {return false};
// document.ondragstart = function() {return false};
// document.onselectstart = function() {return false};
6 changes: 6 additions & 0 deletions js/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function friend_box(obj) {
let id = document.createElement("div");
id.innerHTML = obj.id;
document.querySelector(".result").appendChild(id);

};
20 changes: 20 additions & 0 deletions js/usersearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const form = document.getElementsByTagName("form")[0];
form.addEventListener("submit", function (e) {
e.preventDefault();

if (e.target.search.value != "" || e.target.search.value < -1) {
const url = `/api/search?q=${e.target.search.value}`;

let result = document.getElementsByClassName("result");
fetch(url)
.then(res => res.json())
.then(data => {
data?.forEach(item => {
friend_box(item);
})
});

} else alert("검색어를 입력해라");

// const box = document.getElementById("searchbox").value;
})
5 changes: 5 additions & 0 deletions view/template/footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="userlist"></div>

<script src="/js/profile.js"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions view/template/header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<ul>
<li><a href="/user/usersearch">사용자검색</a></li>
<li><a href="/user/friendlist">친구요청</a></li>
<li><a href="/">HOME</a></li>
<?php if (isset($_SESSION['user'])) { ?>
<li><?php echo $_SESSION['user']->name?>님 환영합니다.</li>
<li><a href="/user/logout">로그아웃</a></li>
<?php } else { ?>
<li><a href="/user/login">로그인</a></li>
<li><a href="/user/join">회원가입</a></li>
<?php } ?>
</ul>
1 change: 1 addition & 0 deletions view/template/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
얼굴책 메인페이지입니다.
63 changes: 63 additions & 0 deletions view/user/join.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<div class="container">
<h1>회원가입</h1>
<form method="post" action="">
<fieldset>
<legend>회원가입 작성 폼</legend>
<input type="hidden" name="action" value="join">
<div class="row">
<div class="col-10 offset-1">
<input type="hidden" name="action" value="join"/>
<div class="row">
<label for="userid" class="col-sm-2col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="userid" name="userid"
placeholder="email을 입력하세요">
</div>
</div>
<div class="row">
<label for="birth" class="col-sm-2col-form-label">BirthYear</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="birthYear" name="birth"
placeholder="생년월일을 입력해주세요">
</div>
</div>
<div class="row">
<label for="password" class="col-sm-2col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password"
placeholder="비밀번호를 입력하세요">
</div>
</div>
<div class="row">
<label for="password_chk" class="col-sm-2col-form-label">Password Check</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password_chk" name="password_chk"
placeholder="비밀번호를 확인해주세요">
</div>
</div>
<div class="row">
<label for="captcha" class="col-sm-2col-form-label">Captcha</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="captcha" id="maincaptcha" oncontextmenu="return false" ondragstart="return false" readonly>
<div id="log"></div>
<input type="text" class="form-control" id="captcha"
placeholder="자동입력방지문자를 입력해주세요">
</div>
</div>
<div class="row">
<label for="username" class="col-sm-2col-form-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username" id="username">
</div>
</div>
<div class="row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">회원가입</button>
</div>
</div>
</div>
</div>
</fieldset>
</form>
</div>
<script src="/js/captcha.js"></script>
24 changes: 24 additions & 0 deletions view/user/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="container">
<h1>로그인</h1>
<div class="container">
<form method="post" action="">
<fieldset>
<legend>로그인 폼 작성</legend>
<input type="hidden" name="action" value="login">
<div class="row">
<div class="col-10 offset-1">
<div class="form-group">
<label for="userid">Email</label>
<input type="email" class="form-control" id="userid" name="userid">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-primary">로그인</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
4 changes: 4 additions & 0 deletions view/user/logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
session_destroy();
alert('로그아웃 되었습니다.');
move('/');
12 changes: 12 additions & 0 deletions view/user/usersearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<form method="post" action="../../actions/userSearch.php">
<label for="search"></label>
<label for="plusfr"></label>
<div class="usersearch">
<input type="hidden" name="action" value="search">
<input type="text" name="search" class="form-control" id="searchbox" placeholder="사용자 검색...">
</div>
<button type="submit" class="searchbtn">검색</button>
</form>
<div class="result"></div>
<script src="/js/profile.js"></script>
<script src="/js/usersearch.js"></script>