Archive for Dev. (junyup2)

์ง€์‹์„ ์ฑ„์›Œ๊ฐ€๋Š” ใ€Ž๊ฐœ๋ฐœ์ž/ํ™”์ดํŠธํ•ด์ปคใ€๋ฅผ ๋ชฉํ‘œ๋กœ ์ •๋ฆฌํ•˜๋Š” ๋ธ”๋กœ๊ทธ

๋ชจ์˜ ํ•ดํ‚น ์Šคํ„ฐ๋””/๋ชจ์˜ ํ•ดํ‚น - ๊ณผ์ œ

[๊ณผ์ œ] 01์ฃผ์ฐจ (๊ฐ„์ด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์ œ์ž‘)

Gearvirus(junyup2) 2023. 11. 1. 17:33

๋ชจ์˜ ํ•ดํ‚น ์Šคํ„ฐ๋”” - 1์ฃผ์ฐจ ๊ณผ์ œ (๊ฐ„์ด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์ œ์ž‘)

๊ฐ„์ด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€

์ œ์ž‘ ์กฐ๊ฑด
  • DB ์—ฐ๊ฒฐ X - ํ•˜๋“œ์ฝ”๋”ฉ ๋ฐฉ์‹
  • ID / PW == admin / admin1234 ์ž…๋ ฅ์‹œ ๋กœ๊ทธ์ธ
    • ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๊พธ๋ฏธ๊ธฐ (CSS / Bootstrap ์ด์šฉ)
    • ์ถ”ํ›„ ์›ํ•˜๋Š”๋Œ€๋กœ ์ ์šฉ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ CSS๋กœ ํ•˜๋‚˜๋ถ€ํ„ฐ ์Œ“์•„ ์˜ฌ๋ฆฌ๋Š”๊ฒƒ์œผ๋กœ ์ œ์ž‘ ์—ฐ์Šต.

index.php
login.php
home.php

 

๋™์ž‘ ๊ณผ์ •
  1. ์ฒซ ํ™”๋ฉด์ธ http://192.168.xxx.xxx:1018/(index.php)์— ์ ‘์†
  2. Login ํด๋ฆญ์‹œ http://192.168.xxx.xxx:1018/login.php๋กœ ์ด๋™
  3. http://192.168.xxx.xxx:1018/login.php ์—์„œ ์˜ฌ๋ฐ”๋ฅธ ID/PW ์ž…๋ ฅ์‹œ
  4. http://192.168.xxx.xxx:1018/home.php ๋กœ ์ด๋™ ํ•˜์—ฌ "Hello, user๋ช…" ์„ ์ถœ๋ ฅ

์ฝ”๋“œ ๋ชฉ๋ก
  • inc/header.php : ๊ณตํ†ต ํ—ค๋”
  • index.php : ์ดˆ๊ธฐ ํ™”๋ฉด
  • login.php : ๋กœ๊ทธ์ธ ํ™”๋ฉด
  • login_proc.php : ๋กœ๊ทธ์ธ ์ •๋ณด ํ™•์ธ
  • home.php : ๋กœ๊ทธ์ธ ์™„๋ฃŒ ํ™”๋ฉด
  • css/style.css : ์ ์šฉํ•  CSS

 

inc/header.php 

<p style='text-align:right'>
	<?php
	if(!session_id()){
		session_start();
	}

	if(!isset($_SESSION['member_id'])){
	?>
	<a href="/login.php">Login</a>
	<?php
	}else{
	?>
	<a href="/index.php">Logout</a>
	<?php
	}
	?>
</p>

 

์ดˆ๊ธฐํ™”๋ฉด์—์„œ ๋กœ๊ทธ์ธ ์ฐฝ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฑฐ๋‚˜, ๋กœ๊ทธ์ธ ์™„๋ฃŒ ์ฐฝ์—์„œ ๋กœ๊ทธ์•„์›ƒ์„ ํ•˜๋Š” ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋ณ€ํ•˜๋Š” ๊ณตํ†ต ํ—ค๋”

 

  • if(!isset($_SESSION['member_id']))
    • $_SESSION['member_id'] ์˜ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ํ‘œ๊ธฐํ•  ํ—ค๋”๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •

Index.php

<?php
session_start();
unset($_SESSION['member_id']);
?>

<!DOCTYPE html>
<html>
	<head>
		<title>LJY</title>
		<link rel="stylesheet" href="css/style.css">
	</head>
	<body>
		<div class="container">
			<h1 class="spacing grad">Index</h1>
			<?php require_once("inc/header.php"); ?>
			<h2>Please, Login</h2>
			<p>Click the Login</p>
		</div>
	</body>
</html>

  • ์‚ฌ์ดํŠธ์˜ ์ฒซ ํ™”๋ฉด, unset($_SESSION['member_id']); ์ƒํƒœ์ด๋ฏ€๋กœ inc/header.php ๋กœ๋ถ€ํ„ฐ Login ํ—ค๋”๋ฅผ ๋ถˆ๋Ÿฌ์˜ด

login.php

<!DOCTYPE html>
<html lang="en">
	<head>
	<meata charset="utf-8">
		<title>Login Page</title>
		<link rel="stylesheet" href="css/style.css">
	</head>
	<body>			
		<div class="container">
			<h1 class="shadow spacing grad">Gearvirus</h1>
				<form method="POST" action="login_proc.php" autocomplete="off">
					<br>
					<label for="user_id">์•„์ด๋””</label>
					<input type="text" name="user_id" placeholder="User ID" autofocus required/>
					<p>
					<label for="user_pw">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
					<input type="password" name="user_pw" placeholder="User Password" required/>
					<br>
					<input type="submit" value="๋กœ๊ทธ์ธ"/>
				</form>
			<br><br>
			<footer class="footer">
				<div class="footer-info">Login Test</div>
			</footer>
		</div>
	</body>
</html>

๋กœ๊ทธ์ธ ํ™”๋ฉด

  • <form method="POST" action="login_proc.php" autocomplete="off">
    • method="POST" : url์— ์ถœ๋ ฅ์„ ์•ˆํ•˜๊ธฐ ์œ„ํ•ด์„œ "POST" ์ด์šฉ
    • action="login_proc.php" : action์€ ํŒŒ์ผ์„ ๋‹ค๋ฅธ ๊ณณ์—์„œ ํ™•์ธ(action์— ์ง€์ •๋˜์–ด ์žˆ๋Š” login_proc.php ๋กœ ์ •๋ณด ์ „๋‹ฌ)
    • autocomplete="off" : ์•„์ด๋”” ์ž…๋ ฅ์‹œ ์ž๋™์™„์„ฑ ๋ฐฉ์ง€
  • <input type="text" name="user_id" placeholder="User ID" autofocus required/>
    • placeholder="User ID" : ๋‚ด์šฉ ์ž…๋ ฅ ํžŒํŠธ
    • autofocus : ์›น ๋ฌธ์„œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค์ž๋งˆ์ž, ํ•ด๋‹น ํ…์ŠคํŠธ ํ•„๋“œ์— ์ž…๋ ฅ ์ปค์„œ๊ฐ€ ์œ„์น˜ํ•˜๋„๋ก ํ•จ.
    • required : ํ•„์ˆ˜ ์ž…๋ ฅ ํ•„๋“œ ์ง€์ •, ๋ฏธ ์ž…๋ ฅ์‹œ ์•„๋ž˜์˜ ๋ฌธ๊ตฌ ์ถœ๋ ฅ

๋ฌธ๊ตฌ


login_proc.php

<?php
	$user_id = $_POST['user_id'];
	$user_pw = $_POST['user_pw'];

	$login_id = "admin";
	$login_pw = "admin1234";

	$is_pw = password_verify($user_pw, $login_pw);
	
	if($user_id == $login_id && $user_pw == $login_pw){
		session_start();
		$_SESSION['member_id'] = $user_id;
		header("Location: /home.php");
		exit();
	}else{
		echo '<script>alert("๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๊ฑฐ๋‚˜ ์ž˜๋ชป ์ž…๋ ฅ"); history.back(-1)</script>';
	}
?>

 

๋กœ๊ทธ์ธ ์‹คํŒจ ๊ฒฝ๊ณ ์ฐฝ

  • if($user_id == $login_id && $user_pw == $login_pw)
    • ๋กœ๊ทธ์ธ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋ชจ๋‘ ๋งž์„ ๊ฒฝ์šฐ, $_SESSION['member_id'] = $user_id; : ์–ด๋–ค ์œ ์ €๊ฐ€ ๋กœ๊ทธ์ธ ํ•˜์˜€๋Š”์ง€ ํŒŒ์•… ํ•˜๊ธฐ ์œ„ํ•จ
    • header("Location: /home.php"); : home.php ๋กœ ์ด๋™
      • ๋ณดํ†ต์€ header("Location: /home.php"); ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ
      • ๋งค๋ฒˆ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์œ„์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Œ
      • ๊ทธ๋Ÿด๋•Œ๋Š” exit;๋ฅผ ํ•œ์ค„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด๋จ
        • exit();
  • echo '<script>alert("๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๊ฑฐ๋‚˜ ์ž˜๋ชป ์ž…๋ ฅ"); history.back(-1)</script>';
    • alert("๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๊ฑฐ๋‚˜ ์ž˜๋ชป ์ž…๋ ฅ"); : ์•„์ด๋”” or ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ƒ๋‹จ์˜ ๊ฒฝ๊ณ ์ฐฝ ์ถœ๋ ฅ
    • history.back(-1) : ์ด์ „ ํŽ˜์ด์ง€๋กœ ์ด๋™
    • ' " " ' ์ˆœ์œผ๋กœ ์ž…๋ ฅ (" ' ' ")๋„ ๊ฐ€๋Šฅ ํ•˜์ง€๋งŒ ์„œ๋กœ ๊ตฌ๋ถ„์„ ์ง€์–ด ์ค˜์•ผ ํ•จ

home.php

<!DOCTYPE html>
<html lang="en">
	<head>
	<meta charset="utf-8">
		<title>Wellcome</title>
		<link rel="stylesheet" href="css/style.css">
	</head>
	<body>
		<div class ="container">
			<h1 class="spacing grad">Home</h1>
			<?php require_once("inc/header.php"); ?>
				<?php 
				$name = $_SESSION['member_id'];
				echo "Hello, " .$name;
				?>
		</div>
	</body>
</html>

 

  • ์‚ฌ์ดํŠธ์˜  ํ™ˆ ํ™”๋ฉด,$_SESSION['member_id'] ๋ฅผ login_proc.php ๋กœ ๋ถ€ํ„ฐ ๋ฐ›์•„์˜จ ์ƒํƒœ์ด๋ฏ€๋กœ inc/header.php ๋กœ๋ถ€ํ„ฐ ใ…ฃLogout ํ—ค๋”๋ฅผ ๋ถˆ๋Ÿฌ์˜ด
  • $name = $_SESSION['member_id']; : login_proc.php ์—์„œ ๋ฐ›์•„์˜จ ์ •๋ณด๋ฅผ ์ €์žฅ
  • echo "Hello, " .$name;
    • .$name; : ์ถœ๋ ฅ์‹œ์—๋Š” .$๋ณ€์ˆ˜๋ช… ์œผ๋กœ ํ‘œ๊ธฐ

style.css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    display: grid;
    place-items: center;
    height: 100vh;
}

.container {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    width: 500px;
    margin: 10px auto;
    padding: 10px;
    border: 1px solid #000;
    border-radius: 10px;
}

h1 {
    padding: 10px;
    background-color: #222;
    color: #fff;
    text-align: center;
}

.shadow {
    text-shadow: 3px 3px 2px #111;
}

.spacing {
    letter-spacing: 0.1em;
}

.grad {
    background: linear-gradient(to right bottom, black, white);
}

input {
    width: 100%;
    padding: 5px;
    border: none;
    border-bottom: 1px solid #111;
    outline: none;
    font-size: 20px;
}

 

  • * : ์ „์ฒด์— ์ ์šฉ
  • .shadow : ํ…์ŠคํŠธ์— ๊ทธ๋ฆผ์ž ํšจ๊ณผ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์†์„ฑ
    • ๊ธฐ๋ณธํ˜• text-shadow: none | <๊ฐ€๋กœ ๊ฑฐ๋ฆฌ> <์„ธ๋กœ ๊ฑฐ๋ฆฌ> <๋ฒˆ์ง ์ •๋„> <์ƒ‰์ƒ>
  • .spacing : ๊ธ€์ž ๊ฐ„๊ฒฉ์„ ์กฐ์ ˆํ•˜๋Š” ์†์„ฑ
  • .grad : ๊ทธ๋ผ๋ฐ์ด์…˜
    • ๊ธฐ๋ณธํ˜• linear-gradient(to <๋ฐฉํ–ฅ> ๋˜๋Š” <๊ฐ๋„>, <์ƒ‰์ƒ ์ค‘์ง€์ >, [<์ƒ‰์ƒ ์ค‘์ง€์ >, ......]);

ํ›„๊ธฐ

 

HTML / PHP / CSS ๋ฅผ ์ „๋ถ€ ์ฒ˜์Œ ํ•ด๋ณด๋Š” ๊ด€๊ณ„๋กœ ํ—ˆ์ ‘ํ•œ ๊ฒฐ๊ณผ๋ฌผ์ด ๋‚˜์˜ด.

์ฐจ์ฐจ ๊ณต๋ถ€๋ฅผ ํ†ตํ•ด, ๊ฒฐ๊ณผ๋ฌผ๊ณผ ๋ธ”๋กœ๊ทธ์˜ ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊ฟ”๋‚˜๊ฐˆ ์˜ˆ์ •.

๋‹ค์–‘ํ•˜๊ฒŒ ๋œจ๋Š” ์˜ค๋ฅ˜ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…์˜ ์ค‘์š”์„ฑ์— ๋Œ€ํ•ด ์ƒ๊ธฐ. (PHP - ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…(Trouble Shooting))

 

์งˆ๋ฌธ ํ™˜์˜, ์ˆ˜์ • ๋ฐ ๋ณด์™„์— ๋Œ€ํ•œ ์ง€์  ํ™˜์˜