Web/PHP

[PHP] 로그인/로그아웃/회원가입

WakaraNai 2021. 11. 6. 02:29
728x90
반응형

Database

 

index.html

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

<head>
</head>

<body>
  <form action="./bakery/back/login.php" method="post">
    <label for="id"> ID </label>
    <input type="text" name="id" /> <br>
    <label for="pw"> Password </label>
    <input type="text" name="pw" /> <br>

    <input type="submit" value="Sign In" name="submit">
  </form>

  <br> <br> <br>

  <form action="./bakery/back/signup.php" method="post">
    <label for="id"> ID </label>
    <input type="text" name="id" /> <br>
    <label for="pw"> Password </label>
    <input type="text" name="pw" /> <br>
    <label for="pwcheck"> same Password </label>
    <input type="text" name="pwcheck" /> <br>
    <label for="name"> Bakery Name </label>
    <input type="text" name="name" /><br>
    <label for="email"> Email </label>
    <input type="text" name="email" /> <br>

    <input type="submit" value="Sign Up" name="submit">
  </form>

  <br> <br> <br>

</body>

</html>

 

login.php

<?php
session_start();
$id = $_POST['id'];
$pw = $_POST['pw'];

$mysqli = mysqli_connect("localhost", "root", "비밀번호", "DB이름");

  
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

$sql_check = "SELECT * FROM user WHERE user_id='$id'";
$result = mysqli_query($mysqli, $sql_check);

if (mysqli_num_rows($result) === 1) {
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    if ($row['pwd']==$pw) {
      $_SESSION['user_id'] = $id;
      if (isset($_SESSION['user_id'])) {
        echo "Success Login";
        sleep(2);
        header('Location: ./front/main.php');

      } else{
        echo "Fail to Session Save";
      }
    }else { 
      echo "wrong id or pw";
    }
} else {
  echo "wrong id or pw";
}

mysqli_close($mysqli);
?>

 

main.php

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

<head>
</head>

<body>

  <?php
session_start();
if (!isset($_SESSION['user_id'])){
  header('Location: index.html');
}
?>


  <form action="./php/logout.php" method="post">
    <input type="submit" value="Logout" name="submit">
  </form>

</body>

</html>

 

logout.php

<?php
session_start();
if (session_destroy()){
  header('Location: index.html');
}
?>

 

 

signup.php

<?php
$id = $_POST['id'];
$pw = $_POST['pw'];
$pwcheck = $_POST['pwcheck'];
$name = $_POST['name'];
$email = $_POST['email'];

if ($pw != $pwcheck){
  echo "비밀번호와 확인 문자열이 서로 다릅니다.";
  echo "<a href=signUp.html>back page</a>";
  exit();
}

if ($id==NULL || $pw == NULL || $name== NULL || $email== NULL){
  echo "빈 칸을 모두 채워주세요";
  exit();
}




// 회원 정보를 db에 저장하기
$mysqli = mysqli_connect("localhost", "root", "비밀번호", "DB이름");

  
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

$sql_check = "SELECT * FROM user WHERE user_id='$id'";
$result = mysqli_query($mysqli, $sql_check);

if (mysqli_num_rows($result) === 1) {
    echo "중복된 아이디입니다";
    exit();
} 


// transaction
mysqli_query($mysqli, "START TRANSACTION");

$last_id = -1;
$sql_signup = "INSERT INTO user (user_id, user_name, pwd, email) VALUES ('$id','$name','$pw','$email')";
$result_signup = mysqli_query($mysqli, $sql_signup);
if ($result_signup) {
  $last_id = mysqli_insert_id($mysqli);
}

$sql_payment = "INSERT INTO payment (id, pay) VALUES ($last_id,0)";
$result_payment = mysqli_query($mysqli, $sql_payment);
$sql_count = "INSERT INTO count (id, count) VALUES ($last_id,0)";
$result_count = mysqli_query($mysqli, $sql_count);


// user, payment, count 테이블에 모두 삽입되었는지 확인하기
if ($result_signup and $result_payment and $result_count){
  echo "sign up success";
  header('Location: ../../index.html');
  mysqli_query($mysqli, "COMMIT");
} else {
  mysqli_query($mysqli, "ROLLBACK");
  echo "fail";
}

mysqli_close($mysqli);
?>

 

728x90
반응형

'Web > PHP' 카테고리의 다른 글

PHP mysqli : Transaction , bindParam  (0) 2021.12.10
[PHP] File Upload  (0) 2021.11.06
SQLite  (0) 2021.10.09
파일 읽고 쓰기 - 간단한 데이터를 다룰 때  (0) 2021.10.09
환경설정 - Apache, PHP  (0) 2021.10.09