웹개발(PHP-Mysql)

[웹 개발] 마이페이지(비밀번호 변경 & 개인정보 수정)

무너박사 2024. 7. 16. 13:59

저번시간에는 게시글 검색기능을 구현하였습니다.

 

https://jamesbexter.tistory.com/entry/%EC%9B%B9-%EA%B0%9C%EB%B0%9C-%EA%B2%80%EC%83%89-%EB%B0%8F-%EC%A0%95%EB%A0%AC-%EA%B8%B0%EB%8A%A5-%EA%B5%AC%ED%98%84

 

[웹 개발] 검색 및 정렬 기능 구현

저번시간에는 CRUD 페이지 구현을 진행하였습니다.https://jamesbexter.tistory.com/entry/%EC%9B%B9-%EA%B0%9C%EB%B0%9C-CRUD%EC%83%9D%EC%84%B1%EC%9D%BD%EA%B8%B0%EC%88%98%EC%A0%95%EC%82%AD%EC%A0%9C-%ED%8E%98%EC%9D%B4%EC%A7%80-%EB%A7%8C%EB%93%A4

jamesbexter.tistory.com

 

 

 

이번시간에는 마이페이지 안에서 내정보를 수정하고 비밀번호도 변경할 수 있는 페이지를 만들어보겠습니다.


기능 구현

mypage.php

 

마이페이지는 이렇게 구성하였습니다.

내 정보를 수정해보겠습니다.

 


 

 

정보가 수정되면 알람이 뜬 후 mypage.php 로 이동하게 됩니다.

mypage.php(내 정보 수정후)

 

 


 

비밀번호를 변경해주겠습니다.

비밀번호 변경

 


 

이후 인증절차를 거쳐 비밀번호가 변경됩니다.

 

인증절차를 통과시 비밀번호 변경후 세션은 폐기하고 index.php 로 돌아가게됩니다.

 

만약 인증절차에 통과를 못할시 index.php 로 이동하게 됩니다.

 

인증절차에 따른 알림 메세지
인증통과(o) 인증통과(x)

 

 

 


코드설명

<mypage.php>

<?php
    include 'dbcon.php';
    session_start(); 

    if(!($_SESSION['loggedin'])){	// 점프해서 mypage.php 로 오는것을 방지.
        header('Location: /index.php');
        exit();
    }

    $sql = "select myinfo from userlog where name='".$_SESSION['username']."'";
    $result = mysqli_query($dbcon,$sql);
    $row=mysqli_fetch_array($result);
?>
<!DOCTYPE html>
<head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="holygrail.css" rel="stylesheet" type = "text/css">
    <link href="div.css" rel="stylesheet" type = "text/css">
    <link rel="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
    <title>마이페이지</title>

    <style>     /*a 태그 스타일시트*/
        a:link {
            color : black;
            text-decoration-line: none;
        }
        a:hover{
            text-decoration-line: underline;
        }

    </style>
</head>


<body>
    <div class ="wrapper">
        <header>
            <div style="text-align: right ;font-weight:bold; transform:translate(-30px,30px)"><a href = "page.php">BACK</a></div>
            <h1 style = "text-align:center ; color: #B86824; font-size:20px ; font-weight:medium"> MY PAGE  </h1>
        </header>
        <nav>
            <p style="text-align:center ; color: #B86824; font-size:20px ; font-weight:medium">My Info</p>
            <div style="height:70%"><div class="login-box">
                이미지 삽입
            </div></div>
            <div style= "transform:translate(0,-90px)">
            <div style="text-align:center ; color: #B86824; font-size:20px ; font-weight:medium">Name : <?php echo $_SESSION['username']; ?> </div>
            <div style="text-align:center ; color: #B86824; font-size:20px ; font-weight:medium">ID&nbsp;&nbsp;: <?php echo $_SESSION['userId']; ?> </div>
            </div>
        </nav>
        <main>
            <div style="font-size:20px">내 정보
            </hr>
                <form action="mypage_update_info.php" method="POST">
                <input style="position:absolute;right:2%;height:25px;width:10%" type = submit class="submit-btn" value="수정">
                <textarea name="myinfo" style="position:absolute;left:0px;width:88%;height:25%;font-size:20px"><?php echo $row['myinfo']; ?></textarea>
            </form>
            </div><p>

            <div style="position:absolute;bottom:0px; left:-25%; width:100%;height:65%" class="login-box">
                    <form action="mypage_update_pass.php" method="POST">
                    <p style="text-align:center; font-weight:bold; font-family:Inter" >비밀번호 변경</p>
                    <p style="text-align:center"><input type="text" name ="ex_pass" placeholder = "이전 비밀번호 " maxlength='30' class="id-form" ></p>
                    <p style="text-align:center"><input type="text" name ="new_pass" placeholder = "새로운 비밀번호" maxlength='30' class="password-form"></p>
                    <p style="text-align:center"><input style="height:30px;width:60%" type = submit class="submit-btn" value="변경"></p>
                    </form>
            </div>
        </main>
        
        
    </div>
</body>
</html>

 

 


 

<mypage_update_info.php>

=내 정보 수정 process=

<?php 
    include 'dbcon.php';

    session_start(); 
    if(!($_SESSION['loggedin'])){	// 점프해서 page.php 로 오는것을 방지.
        header('Location: /index.php');
        exit();
    }

    $content=$_POST['myinfo'];              
    $username=$_SESSION['username'];// DB에서 update 를 위한 인증정보 가져오기

    $sql = "select * from userlog where name='".$username."'";
    $result = mysqli_query($dbcon,$sql);
    $row=mysqli_fetch_array($result);

    if($row['name']==$_SESSION['username']){            //글작성자와 세션의 사용자가 같은지 인증
        $sql = "update userlog set myinfo='".$content."' where name='".$username."'";
        $result = mysqli_query($dbcon,$sql);
            //인증 완료시 게시글 삭제
        echo "<script>alert('내 정보가 수정되었습니다!!!'); 
            location.href='/mypage.php'
            </script>";  
            exit();
    }else{  //인증 X 일때 권한이 없다한 후 index.php 로 리다이렉션
        echo "<script>alert('잘못된 접근입니다.');
        location.href='/index.php'
        </script>";  
        exit();
    }

?>

 


 

<mypage_update_pass.php>

=비밀번호 변경 process=

<?php 

include 'dbcon.php';

if(!session_id()){   //세션 파일 생성여부 확인
    session_start();        //세션 시작 
}

$ex_pass=$_POST['ex_pass'];
$new_pass=$_POST['new_pass'];   //입력값 변수로 설정

$password_hash_old=hash("sha256",$ex_pass);
$password_hash_new=hash("sha256",$new_pass);     //입력받은 비번 hash 처리.

$sql = "select * from userlog where name='".$_SESSION['username']."'";
$result = mysqli_query($dbcon,$sql);
$row=mysqli_fetch_array($result);

if($row['password']==$password_hash_old){//인증시 비밀번호 변경
    $sql = "update userlog set password='".$password_hash_new."' where name='".$_SESSION['username']."'";
    $result = mysqli_query($dbcon,$sql);

     //인증 완료시 세션폐기 처리후 ,비밀번호 변경 확인 알람
     session_destroy();
     echo "<script>alert('비밀번호가 변경되었습니다!'); 
     location.href='/index.php'
     </script>";  
     exit();
}else{
    echo "<script>alert('인증정보가 일치하지 않습니다.'); 
     location.href='/index.php'
     </script>";  
     exit();
}

?>

 

 


긴 글 읽어주셔서 감사합니다!