본문 바로가기

Web/Develop_PHP+MySQL

웹 사이트 만들기 - 로그인 단순 기능 구현

1. index.html

<!DOCTYPE html>
<html lang="ko">

<head>
 	 <meta charset="UTF-8">
 	 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 	 <meta name='viewport' content='width=device-width, intial-scale=1.0'>
  
 	 <title>Alioth's Web Page</title>
</head>

<body>
  	// 로그인 페이지로 이동
	<a href="login.html">로그인</a>
</body>

</html>

1.1. <a> 태그

<a href="login.html">로그인</a>

// href="" : 이동할 링크

2. login.html

<!DOCTYPE html>
<html lang="ko">

<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>Alioth's 로그인 페이지</title>
</head>

<body>
	<h2>로그인</h2>
    
	// 입력 받은 값 전달하기
	<form action="login.php" method="post">
    
    // 사용자 계정 입력 값 받기
		<p>아이디 : <input type="text" name="userid" placeholder="아이디를 입력하세요" required></p>
		<p>비밀번호 : <input type="password" name="userpw" placeholder="비밀번호를 입력하세요" required></p>
		<p><input type="submit" value="로그인"></p>
	</form>
</body>

</html>

2.1. <form> 태그 & <input> 태그

<form action="login.php" method="post">
	<input type="text" name="userid" placeholder="아이디를 입력하세요" required>
	<input type="password" name="userpw" placeholder="비밀번호를 입력하세요" required>
</form>

// input 태그 속성
// type : text, password, email ...
// name : 사용자에게 받은 값의 붙일 이름
// placeholder : 도움말
// required : 꼭 입력할 필드

// form 태그 속성
// acrion : 사용자 입력 값을 전달할 링크
// method : 전달 방식

3. login.php

<!DOCTYPE html>
<html lang="ko">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name='viewport' content='width=device-width, intial-scale=1.0'>
    
    <title>Alioth's Web Page</title>

</head>
<body>
    <?php
    
    // 사용자 입력 값 받아오기
    $userid = $_POST["userid"];
    $userpw = $_POST["userpw"];
    
    // 사용자 입력 값과 admin 계정의 일치여부 확인하기
    if($userid == "admin" && $userpw == "admin1234") {
        echo "$userid 로그인 성공!";
    } else {
        echo "로그인 실패..";
    }
    ?>
</body>

</html>

3.1 php에서 사용자 입력 값 받아오기

$_POST['userid'];
$userid = $_POST['userid'];

// $_A['B']
A = 전달 방식 ex) GET, POST
B = 전달된 값의 이름 // input 태그에서 name 속성