메뉴 건너뛰기

XEDITION

project2018

[WEB] 제타위키 세션 PHP 로그인

proin 2018.05.23 02:02 조회 수 : 0

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']) || !isset($_SESSION['user_name']))

{

echo "<meta http-equiv='refresh' content='0;url=login.php'>";

exit;

}

$user_id = $_SESSION['user_id'];

$user_name = $_SESSION['user_name'];

 

echo "<p>안녕하세요. $user_name($user_id)님</p>";

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

?>

</body>

</html>


login.php

<!doctype html>

<html lang="ko">

<head>

<meta charset="utf-8">

<title>login</title>

</head>

<body>

<form method='post' action='login_ok.php'>

<table>

<tr>

<td>아이디</td>

<td><input type='text' name='user_id' tabindex='1'/></td>

<td rowspan='2'><input type='submit' tabindex='3' value='로그인' style='height:50px'/></td>

</tr>

<tr>

<td>비밀번호</td>

<td><input type='password' name='user_pw' tabindex='2'/></td>

</tr>

</table>

</form>

</body>

</html>


login_ok.php

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv='refresh' content='0;url=main.php'>

<title>login_ok</title>

</head>

<body>

<?php

if(!isset($_POST['user_id']) || !isset($_POST['user_pw'])) exit;

$user_id = $_POST['user_id'];

$user_pw = $_POST['user_pw'];

$members = array('user1'=>array('pw'=>'pw1', 'name'=>'한놈'),

'user2'=>array('pw'=>'pw2', 'name'=>'두시기'),

'user3'=>array('pw'=>'pw3', 'name'=>'석삼'));

 

if(!isset($members[$user_id])) {

echo "<script>alert('아이디 또는 패스워드가 잘못되었습니다.');history.back();</script>";

exit;

}

if($members[$user_id]['pw'] != $user_pw) {

echo "<script>alert('아이디 또는 패스워드가 잘못되었습니다.');history.back();</script>";

exit;

}

session_start();

$_SESSION['user_id'] = $user_id;

$_SESSION['user_name'] = $members[$user_id]['name'];

?>

</body>

</html>


logout.php

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv='refresh' content='0;url=main.php'>

<title>logout</title>

</head>

<body>

<?php

session_start();

session_destroy();

?>

</body>

</html>

 
위로