WebHacking

WebHacking - (11)마이페이지 만들기

풉키홍키 2022. 12. 28. 23:13

로그인 후에 내 정보를 확인할 수 있는 마이페이지를 만들어보자

우선 마이페이지를 확인할려면 로그인이 되어있는 상태여야 하고, 보여지는 정보는 간단하게 계정명, 비밀번호 변경, 이름이다.

 

[mypage.php]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>
    <style>
.text{
                text-align:center;
                padding-top:20px;
                color:#000000
        }
        </style>
 
<?php
session_start();
if($_SESSION['c_id']=='')
{
    echo "로그인부터 하세요";
     ?>
    <div class = text>
        <font style="cursor: hand"onClick="location.href='./index.html'">메인 페이지</font>
        </div>
        <?php
        exit;
}
?>
<?php
$c_id = $_SESSION['c_id'];
$c_name = $_SESSION['c_name'];
?>
 
<head>
        <meta charset = 'utf-8'>
</head>
<body>
    <h2 align="center">마이페이지</h2>
        <form action="mypage_modify.php" method="post">
        <h3 align="center">계정명 : <?=$c_id?></h3>
        <h3 align="center">비밀번호 : <input type="password" name="pass">
        <h3 align="center">이 름 : <?=$c_name?></h3>
        <h3 align="center"><input type="submit" value="비밀번호 변경"></h3>
        </form>
</body>
</html>
 
cs

로그인을 하지않은 상태에서 마이페이지에 접근할 수 없게 조건문을 사용한다. (12 ~ 22번 줄)

로그인 페이지에서 발급받은 세션 ID, 세션 Name 을 변수로 설정해 로그인한 계정 정보를 가져올 수 있게 한다. (25 ~ 26번 줄)

마지막으로 변경할 비밀번호를 입력하면 mypage_modify.php로 변경할 비밀번호를 전송한다. (36번 줄)

 

[mypage_modify.php]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
session_start();
include './db_connect.php';
$c_id = $_SESSION['c_id'];
$pw = $_POST['pass'];
$sql = "update member set pw = '$pw' where id = '$c_id'";
$result = mysqli_query($connect, $sql);
 
if($result) {
    echo "비밀번호 변경 성공";
}
else {
    echo "비밀번호 변경 실패";
    echo $sql;
}
?>
<!DOCTYPE html>
<html>
    <style>
.text{
                text-align:center;
                padding-top:20px;
                color:#000000
        }
        </style>
<div class = text>
        <font style="cursor: hand"onClick="location.href='./index.html'">메인 페이지</font>
        </div>
        </html>
        
cs

mypage.php에서 전달받은 변경할 비밀번호를 UPDATE문을 이용해 DB에서 정보를 변경한다. (4 ~ 7번 줄)

 

이런식으로 간단하게 로그인한 계정의 정보를 확인하고, 비밀번호를 변경할 수 있다.