로그인 php
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="../style_contents.css" type="text/css">
<style>
.login_form {text-align: center;
width: 30%;
margin: 0 auto 100px;
padding: 5px;
background-color:rgb(133, 133, 133);
border-radius: 10px;
}
</style>
</head>
<body>
<iframe src="../head.php" frameborder="0" id="bodyframe" name="body" width="100%"></iframe>
<div id="login_contents" class="contents">
<form action="member_login_check.php" method="POST">
<table>
<tr>
<th colspan="2" style="background-color: #323232" >
<font style="color: white; font-size: 150%;" >LOGIN</font>
</th>
</tr>
<tr>
<th>ID</th>
<td class="input"><input type="text" name="user_id" style="border: 0;" maxlength="12"></td>
</tr>
<tr>
<th>PASSWORD</th>
<td class="input"><input type="password" name="user_pw" style="border: 0;" maxlength="20"></td>
</tr>
</table>
<p>
<input type="submit" value="로그인" class="btn_default btn_gray" >
</p>
</form>
</div>
</body>
</html>
<?php
$id=$_POST["user_id"];
?>
<?php ?>
시작과 끝을 이렇게 해줘야 한다.
$는 슈퍼변수 - php에서 만든 것
= -> 대입한다
오른쪽에 있는 값을 왼쪽으로 저장한다
post방식으로 들어온 user_id라는 변수를 id에 저장한다
<?php
$id=$_REQUEST["user_id"];
$pw=$_GET["user_pw"];
?>
post인지 get인지 모를 때는 request를 적어주면 되지만 좋은 방식은 아니다. 이건 로그인 폼에서 어떤 방식으로 할지 method를 지정하는데 그걸보고 맞춰줘야 함. 사용자가 정보를 보내는 것이 먼저이므로.
id,pw가 있는지 select -> 질의 query해서 응답에 따라 성공, 실패를 결정
$strSQL="select * from member where u_id='".$id."' and u_pass='".$pw."';"; //어떤 정보를 출력할 건지 알고 select문에 변수 저장
php에서 .은 공백없이를 의미한다
.을 적지 않으면 상대의 정보가 출력되어 나올때 스페이스가 같이 출력된다
.을붙여 두개의 데이터를 합쳐서 보여준다
$rs=mysqli_query($conn, $strSQL); //DB에 물어봐주는 함수
$rs_arr=mysqli_fetch_array($rs); //질의 결과를 저장하는 함수 array는 배열이라는 뜻이고 여러 데이터를 rs_arr에 저장하겠다는 말
if($rs_arr){
echo "<script>
location.replace('../index.php');
</script>";
<script>는 html 태그이지만 의미는 자바스크립트를 불러오겠다는 말이다.
<script>뒤부터는 자바스크립트이다.
if($rs_arr){
$_SESSION["user_id"]=$id;
echo "<script>
alert('로그인 되었습니다.');
location.replace('../index.php');
</script>";
$_가 나오는 것을 보면 슈퍼 글로벌 변수라는 것을 알 수 있다
history.back();
</script>";
} //rs_arr 변수에 저장된 값이 있는지 없는지 확인
잘못 입력했을 때 뒤로가기를 넣을 수도 있다.
로그아웃 php
<?php
session_start();
if(!$_SESSION["user_id"]){
echo " <script>
alert('잘못된 요청입니다.');
history.back();
</script>";
} else {
session_destroy();
echo "<script>
alert('로그아웃 되었습니다.');
location.replace('../index.php');
</script>";}
?>
앞에 !를 붙이면 user_id가 없는 사람을 뜻한다
즉 로그인도 하지 않은 사람이 로그아웃을 누르면 잘못된 요청이라고 뜬다
'WEB' 카테고리의 다른 글
web-게시판 만들기 (0) | 2025.06.23 |
---|---|
회원가입 php , javascript (0) | 2025.06.23 |
HTML 기초 - tag (2) | 2025.06.19 |
웹 페이지 만들기 (6) 아파치 보안 설정 (0) | 2025.05.22 |
웹페이지 만들기 (5) 각 페이지 별로 만들기, 상대경로 설정, css연결 (0) | 2025.05.22 |