[Normaltic's 취업반 과제] My Page

2024. 4. 28. 18:22정보보안 및 해킹/Normaltic's 취업반 과제

index.php - mypage 버튼 추가 및 클릭시 userid를 my_psge.php에 전달하면서 redirect

[생략]

<div>
  <button class="my-page-header mypage-btn-outline mypage-btn-skin-outline" name="mypage" value="openmypage">Mypage</button>
  <?php
  if(isset($_POST['mypage'])) {
    header("location: my_page.php?login_id=" . $user_id);
    exit;
  }
  ?>
 </div>
 
 [생략]

my_page.php - mypage 요소 구성 및 전달받은 userid를 통해 현재 어느 아이디로 접속해있는지 확인, 쿼리로 row를 받은 후 text에 입력. 그리고 text부분은 편집할 수 없도록 비활성화(readonly)

<?php
require_once('my_page_func.php');

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Mypage</title>

  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link
    href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100..900&family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap"
    rel="stylesheet">
  <link rel="stylesheet" href="style.css" />
  <link rel="stylesheet" href="font.css" />
</head>

<body class="header">
  <div class="info">
  <h1 class="title-text">Mypage</h1>
  <h3 class="info-text">Mypage</h3>
 

  <?php
  $userid = $_GET['login_id'];
  $row = get_user_info($userid);
  ?>
  
  <label for="userid"> ID : </label><br>
  <input type="text" name="userid"value="<?php echo htmlspecialchars($row['name']); ?>" readonly="readonly"/><br>
  <label for="userpassword"> PASSWORD : </label><br>
  <input type="password" name="userpassword"value="<?php echo htmlspecialchars($row['password']); ?>" readonly="readonly"/><br>
  <label for="userage"> AGE : </label><br>
  <input type="text" name="userage"value="<?php echo htmlspecialchars($row['age']); ?>" readonly="readonly"/><br>
  <label for="useremail"> EMAIL : </label><br>
  <input type="email" name="useremail"value="<?php echo htmlspecialchars($row['email']); ?>" readonly="readonly"/><br>
  <label for="userphone"> PHONENUMBER : </label><br>
  <input type="tel" name="userphone"value="<?php echo htmlspecialchars($row['phone']); ?>" readonly="readonly"/>

  
</div>
</body>
</html>

my_page_func.php - 전달받은 userid를 통해 쿼리로 row를 추출하여 return

<?php
require_once ('connect_db.php');

function get_user_info($username)
{
  if (!connect_db()) {
    die("Connection failed: " . mysqli_connect_error());
}
$table_name = "rame_table";
$sql_query = "SELECT * FROM $table_name WHERE name='$username'";
$sql_res =  mysqli_query(connect_db(), $sql_query);

$row = mysqli_fetch_assoc($sql_res);

return $row;
}

?>