비주얼스튜디오코드

[ 27일차 ] 페이지나누기 후 쿠키, 세션을 가지고 로그인하기(관리자모드)2

ggosoon 2022. 6. 28. 18:02

-현재페이지($idx 필요)/ 전체페이지 찍기 & 처음/이전/다음/마지막 페이지 나누기

list1.php



<?$rootPath=$_SERVER['DOCUMENT_ROOT'];?>

<? include  $rootPath."/D_include/top.php"?>

<?
$conn= new mysqli ("localhost","root","autoset","korea");

$ch1=$_GET['ch1'];
$ch2=$_GET['ch2'];

$page_size=10; //페이지사이즈 설정

if($_GET['idx']==""){  //idx 변수설정
$idx=0;
}else{
$idx=$_GET['idx'];
}

$SQL1="select idx, name, age, title, etc from board0628";
$SQL2=" order by idx desc limit $idx,$page_size";

//전체 레코드 수
$SQL_tc1="select count(*) tc from board0628";

if($ch1==""){
    $SQL=$SQL1.$SQL2;
    $SQL_tc=$SQL_tc1;
}
else if($ch1=="name"){
    $SQL=$SQL1." where name like '%$ch2%'".$SQL2;
    //조건에 맞는 전체 레코드 수
    $SQL_tc=$SQL_tc1." where name like '%$ch2%'";
}
else if($ch1=="title"){
    $SQL=$SQL1." where title like '%$ch2%'".$SQL2;
    //조건에 맞는 전체 레코드 수
    $SQL_tc=$SQL_tc1." where name title like '%$ch2%'";
}
    
     
$result_tc= $conn->query($SQL_tc);
$rs_tc =$result_tc->fetch_assoc();
$tc= $rs_tc['tc'];

$total_page= ceil($tc/ $page_size); //전체페이지
$now_page= ($idx/$page_size)+1;// 현재페이지

$result= $conn->query($SQL);

?>

<section> 
    <br><br>
    <div align=center><font size=4><b>목록보기 (페이지나누기1)</b></font></div>
    <div align=center><br>
    전체레코드: <?=$tc?> &emsp; <?=$now_page?>현재페이지/ <?=$total_page?>전체페이지
    <table border=1 width=500>
    <tr>
        <th>순번</th><th>번호</th><th>이름</th><th>나이</th><th>제목</th><th>특이사항</th>
    </tr>
     <? 
     $i=1;
     while($rs=$result->fetch_assoc()){?>
        
      <tr align=center>
        <td align=center><?=$i?></td>
        <td><?=$rs['idx']?></td>
        <td><?=$rs['name']?> </td>
        <td><?=$rs['age']?></td> 
        <td><?=$rs['title']?></td> 
        <td><?=$rs['etc']?> </td>
     </tr>
     <? $i++; }?> 
     
     <br>
    </table>
    <form>
        <select name=ch1> 
        <option value="name">이름</option>
        <option value="title">제목</option>
        </select>
        <input type= text name=ch2>
        <input type="submit" value="검색하기">
     </form>

     <a href=list1.php?idx=0&ch1=<?=$ch1?>&ch2=<?=$ch2?>>처음</a>&nbsp;&nbsp;

     <? if ($idx == 0){?>
        이전&nbsp;&nbsp;
     <?}else{?>
        <a href=list1.php?idx=<?=$idx-$page_size?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>>이전</a>&nbsp;&nbsp;
        <?}?>

   <? if($total_page!=$now_page){?>
    <a href=list1.php?idx=<?=$idx+$page_size?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>>다음</a>&nbsp;&nbsp;
     <?} else{?>
        다음&nbsp;&nbsp;
        <?}?>

    <?$end_page=($total_page-1)*$page_size ?>
    <a href=list1.php?idx=<?=$end_page?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>>마지막

</div>
</section>

<? include  $rootPath."/D_include/bottom.php"?>

첫페이지
11페이지
마지막 페이지

 

-특이사항에 링크 걸어서 링크 클릭하면 그 레코드 삭제하게 만들기 

list1.php 특이사항에 링크 걸기 

 <td>     <a href=delete.php?delidx=<?=$rs['idx']?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>&idx=<?=$_GET['idx']?>>
       <?=$rs['etc']?>

     </a></td>
 
 
 
 delete.php 만들어서 
 

<? $delidx=$_GET['delidx'];
$idx=$_GET['idx'];
$ch1=$_GET['ch1'];
$ch2=$_GET['ch2'];

$conn= new mysqli ("localhost","root","autoset","korea");

$SQL= "delete from board0628 where idx='$delidx'";

$conn->query($SQL);
$conn->close();
?>

<script>
        location.href="list1.php?idx=<?=$idx?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>"
</script>

list2.php 만들기

list2.php




<? include $_SERVER["DOCUMENT_ROOT"]."/D_include/top.php" ?>

<?

        $conn=new mysqli("localhost","root","autoset","korea");

        $ch1 = $_GET['ch1'];
        $ch2 = $_GET['ch2'];

        //1.페이지사이즈
        $page_size=10;

        //2.페이지List사이즈
        $page_List_size = 10;
        if($_GET['idx']==""){
            $idx=0;
        }
        else{
            $idx=$_GET['idx'];
        }

        $SQL1="select idx, name, age, title, etc from board0628";
        $SQL2=" order by idx desc limit $idx,$page_size";

        //전체 레코드 수
        $SQL_tc1="select count(*) tc from board0628";

        if($ch1==""){
            $SQL=$SQL1.$SQL2;
            $SQL_tc=$SQL_tc1;
        }
        else if($ch1=="name"){
            $SQL=$SQL1." where name like '%$ch2%'".$SQL2;

            //조건에 맞는 전체 레코드 수
            $SQL_tc=$SQL_tc1." where name like '%$ch2%'";
        }
        else if($ch1=="title"){
            $SQL=$SQL1." where title like '%$ch2%'".$SQL2;
            $SQL_tc=$SQL_tc1." where name title like '%$ch2%'";
        }

        $result_tc = $conn -> query($SQL_tc);
        $rs_tc = $result_tc -> fetch_array();
        
        //3.전체레코드수
        $tc = $rs_tc['tc'];
        //4. 총페이지수
        $total_page = ceil($tc/$page_size);
        //5.현재레코드
        $now_record=$idx+1;
        //6. 현재페이지
        $now_page=ceil($now_record/$page_size);
        $result = $conn -> query($SQL);
        //7.하단가로 시작
        $start_page = floor(($now_page-1)/$page_List_size)*$page_List_size+1;
        //8.하단가로 마지막
        $end_page = $start_page+$page_List_size-1;
?>

<section>
    <br>
    <div align=center> <font size=4> <b>목록보기 (페이지나누기1) </b></font>  </div>    
    <div align=center>
    <br>

        1.페이지사이즈 <?=$page_size?> &nbsp;&nbsp;
        2.페이지List사이즈 <?=$page_List_size?> &nbsp;&nbsp;
        3.전체레코드수 <?=$tc?> &nbsp;&nbsp;
        4.총페이지수 <?=$total_page?>  <br> 
        5.현재레코드 <?=$now_record?>&nbsp;&nbsp;
        6.현재페이지 <?=$now_page?>&nbsp;&nbsp;
        7.하단가로시작 <?=$start_page?>&nbsp;&nbsp;
        8.하단가로마지막 <?=$end_page?>&nbsp;&nbsp;


    <TABLE border = 1 width = 500 >
        <tr>
            <td>순번</td><td>번호</td><td>이름</td><td>나이</td><td>제목</td>
            <td>특이사항</td>
        </tr>
        <?
            $i=1;
            while($rs = $result -> fetch_array()){?>
            <tr>
                    <td align=center><?=$i?></td>
                    <td><?=$rs['idx']?></td>
                    <td><?=$rs['name']?></td>
                    <td><?=$rs['age']?></td>
                    <td><?=$rs['title']?></td>
                    <td><a href=delete.php?delidx=<?=$rs['idx']?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>&idx=<?=$_GET['idx']?>><?=$rs['etc']?></a></td>
            </tr>    
        <? $i++;} ?>
    </TABLE>    
   
    <form>
    <select name="ch1">
                <option value = "name">이름</option>
                <option value = "title">제목</option>
    </select>
    <input type = "text" name="ch2">    
    <input type = "submit" values="검색하기">
    </form>
    
<?
    if($idx<$page_size){
        echo "처음&nbsp;&nbsp;"; }
    else{ ?>
    <a href=list2.php?idx=0&ch1=<?=$ch1?>&ch2=<?=$ch2?>>처음&nbsp;&nbsp;</a>
    <? } ?>
    <?
    if($start_page==1){ ?>
        이전<?=$page_List_size?>페이지&nbsp;&nbsp;
    <? }
    else{?>
        <a href=list2.php?idx=<?=($start_page-2)*$page_List_size?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>>이전<?=$page_List_size?>페이지&nbsp;&nbsp;</a>
      <? }


        for($i=$start_page;$i<=$end_page;$i++){
            if($i<=$total_page){
                ?>
                    <a href=list2.php?idx=<?=($i-1)*$page_size?>><?=$i?></a>&nbsp;&nbsp;
            <? }
        }
    ?>

    <?
    if($end_page < $total_page){
        ?>
        <a href=list2.php?idx=<?=$end_page*$page_List_size?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>>다음<?=$page_List_size?>페이지&nbsp;&nbsp;</a>
    <? }
    else{ ?>
        다음<?=$page_List_size?>페이지&nbsp;&nbsp;
    <? } ?>
        
    <?
    $endPage=($total_page-1)*$page_size;
    if($now_page!=$total_page){ ?>
        <a href=list2.php?idx=<?=$endPage?>&ch1=<?=$ch1?>&ch2=<?=$ch2?>>마지막&nbsp;&nbsp;</a>
    <? } 
    else{ 
        echo "마지막&nbsp;&nbsp;";
     } ?>
    </div>
</section>


<? include  $rootPath."/D_include/bottom.php"?>


-세션으로 로그인창 만들기 

세션: 세션은 한 번 변수에 값을 발생 시키면 세션을 종료시키든지 아니면 브라우저를 닫을 때까지 값을 유지한다. 세션의 설정시간까지 유지가 된다.

session_start() 는 맨 위에다가 적어준다

login.php




<?$rootPath=$_SERVER['DOCUMENT_ROOT'];?>

<? include  $rootPath."/D_include/top.php"?>

<section> 
    <br><br>
    <div align=center><font size=4><b>로그인</b></font></div>
    <div align=center><br>
     <form action=login_ok.php>
        <table border=1>
            <tr>
                <td>아이디</td><td><input type=text name=id></td>
            </tr>
            <tr>
                <td>암호</td><td><input type=text name=pwd></td>
            </tr>
            <tr>
                <td colspan=2 align=center><input type=submit value="로그인">
     <br>
</table>
</div>
</section>

<? include  $rootPath."/D_include/bottom.php"?>

 

<? session_start(); ?>

<?$rootPath=$_SERVER['DOCUMENT_ROOT'];?>

<? include  $rootPath."/D_include/top.php"?>

<section> 
    <br><br>
 <div align=center>   
    <?

$id = $_GET['id'];
$pwd = $_GET['pwd'];

if($id == "admin"){
   if($pwd == "1234" ){
    $_SESSION["id"] = "admin";
?>
<script> alert('로그인 성공');
 location.href="http://127.0.0.1/index.php"; 
 </script>



<?
     
}else {
    ?>
    <script> alert('로그인(암호확인) 실패'); 
    location.href="login.php";
    </script>
<?   
}
} else {
?>
  <h1>로그인 실패 </h1>
  <a href="<?=$path?>/C_admin/login.php">로그인</a>
<?
}
?>

<br>
</div>
</section>

<? include  $rootPath."/D_include/bottom.php"?>

 

logout.php




<? session_start();?>

<?$rootPath=$_SERVER['DOCUMENT_ROOT'];?>

<? include  $rootPath."/D_include/top.php"?>

<section> 
    <br><br>
    <div align=center><font size=4><b>로그아웃</b></font></div>
     <br><br><br><br>
     <h1 align=center> 로그아웃 완료!!!! </h1>
     <?
     $_SESSION["id"] == " "; //값을 공백으로 변경
     session_unset(); //세션 비우기
     session_destroy(); //세션 제거하기 

     header('Location:http://127.0.0.1/index.php');
     exit;  
     ?>
     <br>
</section>

<? include  $rootPath."/D_include/bottom.php"?>

 

여기까지 전체코드


top.php



 <? if( $_SESSION["id"] == "admin"){?> 
        <a href=<?=$path?>/B_big/insert.php>빅데이터추가</a>&nbsp;
        <a href=<?=$path?>/B_big/list1.php>페이지나누기1</a>&nbsp;
        <a href=<?=$path?>/B_big/list2.php>페이지나누기2</a>&emsp;&emsp;
        <? }?>
        
        
    하면 위에 3개 목록은 로그인 전까지 안보인다

 

아래 top.php 전체코드 

top.php



// 제일 위에 적어준다 
<? session_start(); ?>


<html>
<head>
    <? 
    $host= $_SERVER['HTTP_HOST'];
    $path="http://".$host;
    $rootPath=$_SERVER['DOCUMENT_ROOT'];
    ?>
<style>
header{
background-color:lightpink;
height:65px;
line-height:65px;
color:white;
font-size:30px;
text-align:center;
}
nav{
    background-color:hotpink;
    height:30px;
    line-height:30px;
    color:white;
    font-size:13px;
}
section{
    background-color:coral;
    min-height:530px;
    font-size:px;
}
footer{
    background-color:lightpink;
    height:40px;
    line-height:40px;
    color:white;
    font-size:15px;
    text-align:center;
}
#body{
width:90%;
margin:auto;
}

/*링크 라인 빼기*/
a{text-decoration-line:none;}

/*링크 라인 빼기& 글씨색 하얀색*/
a:link, a:visited{
    color:#ffffff;
    text-decoration:none;
}

/*마우스 올려놓으면 링크 라인& 글씨색 검정색*/
a:hover, a:active{
    color:#000000;
    text-decoration-line:underline;
}

</style>
</head>
<body>
<div id="body">
<header>학생 성적처리 프로그램 Ver.1.11</header>
<nav>
        &emsp;&emsp;&emsp;
        <a href=<?=$path?>/A_school/form.php>학생등록</a>&nbsp;
        <a href=<?=$path?>/A_school/list.php>학생목록/수정</a>&emsp;&emsp;

        <? if( $_SESSION["id"] == "admin"){?> 
        <a href=<?=$path?>/B_big/insert.php>빅데이터추가</a>&nbsp;
        <a href=<?=$path?>/B_big/list1.php>페이지나누기1</a>&nbsp;
        <a href=<?=$path?>/B_big/list2.php>페이지나누기2</a>&emsp;&emsp;
        <? }?>

        <? if($_SESSION["id"]!="admin"){ ?>
        <a href="<?=$path?>/C_admin/login.php">로그인</a>&nbsp;&nbsp;
        <? } else { ?> 
        <a href="<?=$path?>/C_admin/logout.php">로그아웃</a>&emsp;&emsp;
        <? } ?>

        <a href=<?=$path?>/index.php>홈으로</a>
</nav>

첫화면

 

로그인 클릭화면
admin / 1234 로 접속한 화면 , 로그아웃을 누르면 첫 화면으로 돌아간다

 

admin 1111 로 접속했을 경우
아이디, 비밀번호 다 틀렸을 경우