메뉴 건너뛰기

XEDITION

project2018

[WEB] 세션 PHP 로그인

proin 2018.05.23 01:52 조회 수 : 2

main.php

<!doctype html>

<html lang="ko">

<head>

<meta charset="utf-8">

<title>main</title>

</head>

<body>

<?php

session_start();

if(!isset($_SESSION['user_id']))

{

header('Location: login.html');

}

 

echo "홈(로그인 성공)";

 

echo "<a href=logout.php>로그아웃</a>";

?>

</body>

</html>


login_check.php

<!doctype html>

<html lang="ko">

<head>

<meta charset="utf-8">

<title>login_check</title>

</head>

<body>

<?php

session_start();

$id = $_POST['id'];

$pw = $_POST['pw'];

$mysqli = mysqli_connect("localhost", "root", "pass", "test");

 

$check = "SELECT * FROM user WHERE user_id = '$id'";

$result = $mysqli -> query($check);

if($result -> num_rows == 1)

{

$row = $result -> fetch_array(MYSQLI_ASSOC);

if($row['user_password'] == $pw)

{

$_SESSION['user_id'] = $id;

if(isset($_SESSION['user_id']))

{

echo "성공";

sleep(3);

header('Location: main.php');

}

else

{

echo "세션 저장 실패";

}

}

else

{

echo "아이디 또는 비밀번호가 틀립니다";

}

}

else

{

echo "아이디 또는 비밀번호가 틀립니다";

}

?>

</body>

</html>


logout.php

<!doctype html>

<html lang="ko">

<head>

<meta charset="utf-8">

<title>logout</title>

</head>

<body>

<?php

session_start();

$res = session_destroy();

if($res)

{

header('Location: ./main.php');

}

?>

</body>

</html>


signup.html

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>Sign Up</title>

</head>

<body>

<form action="./signup.php" method="post">

<div>

<label for="id"> ID </label>

<input type="text" name="id" />

</div>

<div>

<label for="pw"> PW </label>

<input type="password" name="pw" />

</div>

<div>

<label for="pwc"> PWC </label>

<input type="password" name="pwc" />

</div>

 

<div>

<label for="name"> Name </label>

<input type="text" name="name" />

</div>

<div>

<label for="email"> E-mail </label>

<input type="email" name="email" />

</div>

<div class="button">

<input type="submit" value="submit">

</div>

</form>

</body>

</html>


signup.php

<!doctype html>

<html lang="ko">

<head>

<meta charset="utf-8">

<title>signUp</title>

</head>

<body>

<?php

$id = $_POST['id'];

$pw = $_POST['pw'];

$pwc = $_POST['pwc'];

$name = $_POST['name'];

$email = $_POST['email'];

 

if($pw != $pwc)

{

echo "비밀번호와 비밀번호 확인이 서로 다릅니다.";

echo "<a href=signup.html>bank page</a>";

exit();

}

 

if($id == NULL || $pw == NULL || $name == NULL || $email == NULL)

{

echo "빈 칸을 모두 채워주세요";

echo "<a href=signup.html>back page</a>";

exit();

}

 

$mysqli = mysqli_connect("localhost", "root", "pass", "test");

 

$check = "SELECT * FROM user WHERE user_id='$id'";

$result = $mysqli -> query($check);

if($result -> num_rows == 1)

{

echo "중복된 id입니다.";

echo "<a href=signup.html>back page</a>";

exit();

}

 

$signup = mysqli_query($mysqli, "INSERT INTO user (user_id, user_pw, name, email) VALUES('$id', '$pw', '$name', '$email')");

if($signup)

{

echo "sign up success";

}

?>

</body>

</html>


login.html

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>login page</title>

</head>

<body>

<form action="login_check.php" method="post">

<div>

<label for="id">ID </label>

<input type="text" name="id" />

</div>

<div>

<label for="pw">PW </label>

<input type="password" name="pw" />

</div>

 

<div claass="button">

<button type="submit"> login </button>

</div>

</form>

<button onClick="location.href='signUp.html'"> sign up </button>

</body>

</html>


 

위로