php) 로그인(세션_아이디저장) 글작성자(세션_아이디) 게시판 조회수

login_form.php

<html lang="ko">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>sample page</title>
    </head>
    <body>
        <h1>login PHP!_MySQL</h1>
    <form method="post" action="./login_result.php">
        <input type="text" name="input_uid" value="ID">
        <input type="password" name="input_pwd">
        <input type="submit">
</form>
</body>
</html>

login_result.php

<?php
$uid = $_POST['input_uid'];
$pwd = $_POST['input_pwd'];
$host = 'localhost:3306';
$user = 'root';
$pw = '<password>';
$dbName = '<dbname>';
$conn = new mysqli($host, $user, $pw, $dbName);

$sql = "SELECT * FROM user WHERE uid='$uid' ";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)){
    if($row['pwd']==$pwd){
        // 발급된 세션 id가 있다면 세션의 id를, 없다면 false 반환
        if(!session_id()) {
        // id가 없을 경우 세션 시작
            session_start();
        }
        $_SESSION["uid"]=$uid; ?>
        <script>
            alert("<?php echo "alert: 로그인 성공 session id:".$_SESSION["uid"] ?>");
            location.replace("<?php echo './list.php' ?>");
        </script>
<?php
    } else { ?>
        <script>
            alert("로그인 실패");
        </script>
        <?php
    }
}
    $conn->close();


    ?>

writing.php

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>BBS demo</title>
</head>
<body>
    <form method="post" action="writing_action.php">
        <h1>writing</h1>
        <h3>title<input type="text" name="title"></h3>
        <h3>content</h3><br>
        <textarea rows="7" cols="33" name="content"></textarea></br>
        <button type="submit">submit</button>
        <button type="reset">reset</button>
    </form>
</body>
</html>

writing_action.php

<?php
session_start();
$title=$_POST['title'];
$writer=$_SESSION["uid"];
$content=$_POST['content'];
// mysql 연결
$host = "localhost";
$user = "root";
$pw = "<password>";
$dbName = "<dbname>";
$conn = new mysqli($host, $user, $pw, $dbName);
//INSERT
# $sql = "INSERT INTO BBS VALUES(NULL ,$title,$content,$writer,NOW(),1)";
$sql = "INSERT INTO BBS(title,writer,content,publish) VALUES('$title','$writer','$content',NOW())";
if ($conn->query($sql) === TRUE) { ?>
<script>
    alert("<?php echo "alert: 게시글이 등록되었습니다.작성자:".$_SESSION["uid"] ?>");
    location.replace("<?php echo './list.php' ?>");
</script>
<?php
} else { ?>
    <script>
        alert("<?php echo "Error: " . $sql . "<br>" . $conn->error; ?>")
    </script>
<?php
}
$conn->close();
?>

list_detail.php

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>BBS demo</title>
</head>
<body>
    <?php
    $id=$_GET['id'];
    // mysql 연결
    $host = 'localhost:3306';
    $user = 'root';
    $pw = '<password>';
    $dbName = '<dbname>';
    $conn = new mysqli($host, $user, $pw, $dbName);
    //view_cnt
    $view_cnt_sql = "UPDATE BBS set view_cnt = view_cnt + 1 WHERE id = $id";
    $conn->query($view_cnt_sql);
    $sql = "SELECT * FROM BBS WHERE id=$id";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_array($result)){

    ?>
    <button onclick=location.href="./list.php">list</button>
    <button onclick=location.href="./edit.php?id=<?php echo $id ?>">edit</button>
    <div>
       <div>
        <span><?php  echo $row['id']; ?></span>
            <span>제목</span><span><?php  echo $row['title']; ?></span>
       <span>조회수</span><span><?php  echo $row['view_cnt']; ?></span>
       </div>
        <div>
           <span>작성자</span><span><?php  echo $row['writer']; ?></span>
           <span>작성일</span><span><?php  echo $row['publish']; ?></span>
        </div>
       <div>내용</div><div><?php  echo $row['content']; ?></div>

    </div>

    <?php
        }
        $conn->close();
    ?>
</body>
</html>
Comment