2017-12-12 1 views
-1

사용자가 로그인 및 로그 아웃하고 다른 기본 기능을 사용하는 간단한 웹 사이트 코드를 작성하고 있습니다. 누구든지 로그인 할 때 로그 아웃 버튼이 방문하는 모든 페이지에 표시되고 로그되지 않은 경우 숨겨지기를 바랍니다. 나는 아직도 새롭고 잘못된 점을 알아 내지 못합니다. 로그인 버튼을 처음 클릭하자마자 로그 아웃 버튼이 나타나지만 다른 페이지로 이동하면 버튼이 내 메뉴에서 사라집니다.PHP는 로그인 한 경우에만 로그 아웃 버튼을 보여줍니다.

menu.php

$currentfile = basename($_SERVER['PHP_SELF']); 
if($currentfile = basename($_SERVER['PHP_SELF'])){ 
    if (isset($_SESSION['ID'])) { 
     echo "<a href=\"index.php\">Home</a> 
     <a href=\"viewlist.php\">See Reviews</a> 
     <a href=\"example11.php\">Write a Review</a> 
     <a href=\"search.php\">Search</a> 
     <a href='logoutconfirmation.php'>Logout</a> 
     <hr />"; 
    }else{ 
     echo "<a href=\"index.php\">Home</a> 
     <a href=\"viewlist.php\">See Reviews</a> 
     <a href=\"example11.php\">Write a Review</a> 
     <a href=\"search.php\">Search</a> 
     <hr />"; 
    } 
} ?> 

index.php를

<?php 
    include "header.inc.php"; 
    $pagetitle= "Login Form"; 
    $showform =1; 
    $errormsg =0; 
    $errorusername = $errorpassword = ""; 
    $inputdate = time(); 

    //FIRST CHECK TO SEE IF THE USER IS LOGGED IN 
    if(isset($_SESSION['ID'])) 
    { 
     echo "<p class='error'> You are already logged in. </p>"; 
     include_once "footer.inc.php"; 
     exit(); 
    } 
    if(isset ($_POST['submit'])) { 

     /************************************************************* 
     * ALL FIELDS- STORE USER DATA; SANITIZE USER-ENTERED DATA 
     *************************************************************/ 
     $formfield['username'] = trim($_POST['username']); 
     $formfield['password'] = trim($_POST['password']); 

     if (empty($formfield['username'])) { 
      $errorusername = "The username is required."; 
      $errormsg = 1; 
     } 
     if (empty($formfield['password'])) { 
      $errorpassword = "The password is required."; 
      $errormsg = 1; 
     } 

     if ($errormsg != 0) { 
      echo "<p class='error'> THERE ARE ERRORS!</p>"; 
     } else { 
      //get the user data from the database 
      try { 
       $user = "SELECT * FROM Users WHERE username =:username"; 
       $stmt = $pdo->prepare($user); 
       $stmt->bindValue(':username', $formfield['username']); 
       $stmt->execute(); 
       $row = $stmt->fetch(); 
       $countuser = $stmt->rowCount(); 

       // if query okay, see if there is a result 
       if ($countuser < 1) { 
        echo "<p class='error'> *This user cannot be found in the 
database* </p>"; 
       } else { 
        if (password_verify($formfield['password'], $row['password'])) { 
        $_SESSION['ID'] = $row['ID']; 
        $showform = 0; 
        header("LocationL confirm.php?state=2"); 
        echo "<p> Thank you for logging in! </p>"; 
       } else { 
        echo "<p class='error'> The username and password 
combinations you entered are not correct. Please try again! </p>"; 
       } 
      }//username exists 
     } catch (PDOException $e) { 
      echo 'ERROR fetching users: ' . $e->getMessage(); 
      exit(); 
     } 
    } 
} 
if($showform == 1) { 
    ?> 
    <p class="homemsg">Welcome to the Movie Review Hub! Feel free to look 
around or sign in to write your own review.</p> 

    <form name="login" id="login" method="post" action="index.php"> 
     <table class="center"> 
      <tr> 
       <th><label for="username">Username: </label></th> 
       <td><input name="username" id="username" type="text" placeholder="Required Username" 
        }?><span class="error" <?php if (isset($errorusername)) { 
       echo $errorusername; 
      } ?></span></td> 
    </tr> 
    <tr> 
     <th><label for="password">Password: </label></th> 
     <td><input name="password" id="password" type="password" placeholder="Required Password" 
        }?><span class="error"> <?php if (isset($errorpassword)) { 
       echo $errorpassword; 
      } ?></span></td> 
     <tr> 
      <th><label for="submit">Submit: </label></th> 
      <td><input type="submit" name="submit" id="submit" value="submit"/></td> 
     </tr> 
    </table> 


    <p><a href=index.php>Register.</a></p> 

    <?php 
    include_once "footer.inc.php"; 
} 
?> 

나는 누군가가에서 로그인 할 경우 내가 모든 페이지에 쇼로 로그 아웃 버튼을 싶습니다 말했듯 색인 페이지는 메뉴가 내 모든 파일에 포함되어 있습니다

로그 아웃 버튼은 처음에는 내가 로그인 버튼을 누르면 보여 주지만 페이지를 새로 고치거나 다른 페이지로 이동하면 사라집니다.

+1

'session_start' 호출은 어디에 있습니까? – CBroe

+2

필자는 칭찬을하기 위해 잠깐 시간을 내고 싶습니다. 실제로 PDO와 준비된 쿼리와 같은 우수 사례를 사용하고 비밀번호를 처리하기 위해'password_verify '를 사용하는 "매우 새로운"프로그래머를 보는 것은 거의 없습니다. 잘 했어. –

답변

1

@CBroe 언급처럼 모든 페이지의 처음에 session_start을 추가하십시오.

더 나은 설정 파일을 만들고 여기에 넣고 모든 것을 포함하십시오.

+0

내 config 파일에 session_start를 추가했는데 내 문제의 절반이 수정되었습니다! 이제는 어떻게 할 수 있는지 알아야합니다. 내 로그 아웃 버튼은 누군가 내 로그인 한 경우에만 표시됩니다. 내 메뉴 파일에서이 작업을 시도했지만 작동하지 않습니다. – bccheatha

+0

당신이 얻는 것을보기 위해 var_dump ($ _ SESSION)을 시도하십시오. –

관련 문제