1.login.html |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div class="layer"> <form method="post" action="./login.php"> <table> <tr> <td>id :<input type="text" class="itext" name="id" value=""> </td> </tr> <tr> <td>pw:<input type="password" name="pw"> </td> </tr> <tr> <td align="center"> <button>login</button> <!-- <input type="submit" value="login"> --> </td> </tr> </table> </form> <a href='member.html'><p style="">회원가입하기</p></a> </div> </body> |
중앙 정렬을 위해 css 사용
style.css |
.layer { position: absolute; top: 40%; left: 40%; width: 200px; height: 200px;
} |
#login.html의 변수를 통해 php의 $id, $pw값지정 login.php |
<?php session_start(); $id = $_POST ["id"]; $pw = $_POST ["pw"]; $connect = mysql_connect ( 'localhost:3307', 'root', '111111' ); if (! $connect) { die ( 'Could not connect: ' . mysql_error () ); } echo 'Connected successfully'; echo '<br/>'; // 한글 깨짐 방지 mysql_query ( "set names euckr" ); mysql_select_db ( "login" ); $query = "select * from member where id='$id'"; $result = mysql_query ( $query, $connect ); // 연관 색인 및 숫자 색인으로 된 배열로 결과 행을 반환 $data = mysql_fetch_array ( $result, MYSQL_NUM ); if ($data [0] == $id) { echo "no erorr id = $id <br/>"; if ($data [1] == $pw) { echo "no error pw = $pw <br/>"; $_SESSION['id']=$id; $_SESSION['is_logged']=1;
header("Location: main.php"); exit(); }else{ //에러메세지 출력 문제 echo "<script>alert(\"실패 \"); history.back(1); </script>"; $_SESSION['is_logged']=2;
}
}else{ $_SESSION['is_logged']=2; echo "<script>alert(\"실패 \"); history.back(1); </script>"; } mysql_close ( $connect ); ?> |
mysql_connect() : DB 접속
mysql_select_db() : table 지정
mysql_query() : 쿼리문 실행
mysql_fetch_array ( $result, MYSQL_NUM ); 위의 실행한 쿼리문을 배열로 받는다
if문
쿼리문의 값과 입력한 값을 비교후 성공하면 세션값을 지정한후 main.php로
실패시 자바스크립트를 통해 에러메세지 출력후 뒤의 화면으로 전환
member.html 로그인창에서 회원가입 클릭시 전환 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div class="layer"> <form method="post" action="./login.php"> <table> <tr> <td>id :<input type="text" class="itext" name="id" value=""> </td> </tr> <tr> <td>pw:<input type="password" name="pw"> </td> </tr> <tr> <td align="center"> <button>login</button> <!-- <input type="submit" value="login"> --> </td> </tr> </table> </form> <a href='member.html'><p style="">회원가입하기</p></a> </div> </body> |
login.php |
<?php session_start(); $id = $_POST ["id"]; $pw = $_POST ["pw"]; $connect = mysql_connect ( 'localhost:3307', 'root', '111111' ); if (! $connect) { die ( 'Could not connect: ' . mysql_error () ); } echo 'Connected successfully'; echo '<br/>'; // 한글 깨짐 방지 mysql_query ( "set names euckr" ); mysql_select_db ( "login" ); $query = "select * from member where id='$id'"; $result = mysql_query ( $query, $connect ); // 연관 색인 및 숫자 색인으로 된 배열로 결과 행을 반환 $data = mysql_fetch_array ( $result, MYSQL_NUM ); if ($data [0] == $id) { echo "no erorr id = $id <br/>"; if ($data [1] == $pw) { echo "no error pw = $pw <br/>"; $_SESSION['id']=$id; $_SESSION['is_logged']=1;
header("Location: main.php"); exit(); }else{ //에러메세지 출력 문제 echo "<script>alert(\"실패 \"); history.back(1); </script>"; $_SESSION['is_logged']=2;
}
}else{ $_SESSION['is_logged']=2; echo "<script>alert(\"실패 \"); history.back(1); </script>"; } mysql_close ( $connect ); ?> |
mysql_query()를 이용해 insert 문을 DB에 입력시키고
세션값을 추가한 뒤 main.php 로 이동
main.php |
<?php session_start(); ?> <html> <head> <title>메인페이지</title> </head> <BODY BGCOLOR="#006699" LINK="#99CCFF" VLINK="#99CCCC" TEXT="#FFFFFF"> 메인페이지<br/> </body> <?php if(($_SESSION['is_logged']==1)){ $id = $_SESSION['id']; echo "$id 로그인 되었습니다"; }else{ echo "로그인 실패"; } session_destroy(); ?> |
세션값을 통해 로그인 확인
확인후 세션 종료