index.php
<?
session_start();
//print_r($_SESSION);
?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?if ($_SESSION) {?>
<a href="logout.php">logout</a>
<?
} else {
?>
<a href="join.php">join</a>
<a href="login.php">Login</a>
<?
}
?>
</body>
</html>
login.php
<!DOCTYPE HTML>
<!DOCTYPE HTML>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<head>
<title> login </title>
</head>
<body>
<form name="frm" action="logindata.php" method="post" onsubmit="return id_chk();">
<div>
로그인하기
<ul>
<li>ID : <input type="text" name="id"><br/></li>
<li>PASS : <input type="password" name="pass"></li>
<li>
<button>확인</button>
</li>
</ul>
</div>
</form>
</body>
</html>
logindata.php
<?
//세션을 사용하기 위해 반드시 실행
session_start();
$db = mysql_connect("localhost", "아이디", "암호");
mysql_select_db("아이디");
$id = $_POST['id'];
$pass = $_POST['pass'];
$query = "select * from test_member WHERE user_id = '$id' and pass = '$pass'";
$result = mysql_query($query);
$list = mysql_fetch_array($result);
//print_r($list);
if($list['user_id']) {
$_SESSION['user_id'] = $list[user_id];
echo $_SESSION['user_id']."님은 로그인 되었습니다.";
// print_r ($_SESSION);
} else {
echo "아이디와 비밀번호가 틀립니다.";
echo " <a href='join.php'>회원가입</a>";
}
?>
join.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title> login </title>
</head>
<body>
<form action="joindata.php" method="post">
<div>
가입하기
<ul>
<li>ID : <input type="text" name="id"><br/></li>
<li>PASS : <input type="password" name="pass"></li>
<li>이름 : <input type="text" name="name"><br/></li>
<li>
<button>확인</button>
</li>
</ul>
</div>
</form>
</body>
</html>
joindata.php
<?
mysql_connect("localhost", "아이디", "암호");
mysql_select_db("아이디");
$query = "insert into test_member set
name = '$_POST[name]',
id = '$_POST[id]',
pass = '$_POST[pass]'
";
$result = mysql_query($query);
//print_r($query);
if($result) {
?>
<script>
alert("OK");
location.href="login.php";
</script>
<?
}
?>
logout.php
<?
session_start();
session_destroy();
?>
좀 더 공부해야 할 듯
'PROGRAM > PHP' 카테고리의 다른 글
PHP 랜덤함수 (0) | 2013.01.16 |
---|---|
switch문 기초 (0) | 2013.01.11 |
PHP DB연결 (0) | 2013.01.07 |
연관배열 for 및 foreach 사용 (0) | 2012.12.17 |
if문 on, off (0) | 2012.12.13 |