2016-07-13 9 views
-3

私は1つの不思議なことを解決することができませんでしたが、今私は何とかそれを解決したので、問題がどこにあるのか尋ねたいと思います。私が書いたPHP - 同じもの異なる出力(他の場合)

最初のコードは、このいずれかであり、それは働いていませんでした。

<?php 
    if(! isset($_SESSION['user'])) 
    { 
?> 
    <main> 
     <div class="container"> 
      <form class="login" method="post" action="../login/login.php"> 
       <input type="text" placeholder="Username" name="username"/><br> 
       <input type="text" placeholder="Password" name="password"/><br> 
       <?php 
        if(isset($_SESSION['error'])) 
         echo $error; 
       ?> 
       <input type="submit" value="Login" name="submit"/><br> 
      </form> 
     </div> 
    </main> 
<?php } ?> 

<?php else 
     { 
      unset($_SESSION['error']); 
?> 
    <header> 
     <div class="container"> 
      <h1>PHP Quizzer</h1> 
     </div> 
    </header> 

    <main> 
     <div class="container"> 
      <h2>This is a PHP quizzer.</h2> 
      <p>This is a multiple choice quiz to test your knowledge of PHP</p> 
      <ul> 
       <li><strong>Number of Questions: </strong></li> 
       <li><strong>Type: </strong>Multiple Choice</li> 
       <li><strong>Estimated Time: </strong></li> 
      </ul> 
      <a href="questions.php?n=1" class="start">Start Quiz</a> 
     </div> 
    </main> 
<?php } ?> 

そして私はちょうど書かれている第二のコードを動作するものであると私は知っていない理由:

<?php 
    if(! isset($_SESSION['user'])) 
    { 
?> 
    <main> 
     <div class="container"> 
      <form class="login" method="post" action="../login/login.php"> 
       <input type="text" placeholder="Username" name="username"/><br> 
       <input type="text" placeholder="Password" name="password"/><br> 
       <?php 
        if(isset($_SESSION['error'])) 
         echo $error; 
       ?> 
       <input type="submit" value="Login" name="submit"/><br> 
      </form> 
     </div> 
    </main> 
<?php } 
    else 
     { 
      unset($_SESSION['error']); 
?> 
    <header> 
     <div class="container"> 
      <h1>PHP Quizzer</h1> 
     </div> 
    </header> 

    <main> 
     <div class="container"> 
      <h2>This is a PHP quizzer.</h2> 
      <p>This is a multiple choice quiz to test your knowledge of PHP</p> 
      <ul> 
       <li><strong>Number of Questions: </strong></li> 
       <li><strong>Type: </strong>Multiple Choice</li> 
       <li><strong>Estimated Time: </strong></li> 
      </ul> 
      <a href="questions.php?n=1" class="start">Start Quiz</a> 
     </div> 
    </main> 
<?php } ?> 

あなたはどこに問題があるのか​​教えてもらえますか?私がした唯一のことは、コードの途中で}else{ unset($_SESSION['error']))をマージしたことです。

EDIT:最初のコードは完全に空白のページを出力します。 2番目のコードは、コードに書かれているものを正確に出力します。

+0

あなたはBitbucket.comとSourceTreeアプリケーションなどのサービスを使用する場合、それはあなたが後にセーブお知らせします/コミットします。 – Kray

+0

@Leviありがとう、私はそれを試みるつもりです。 – scarface

+0

問題ありません。 Bitbucket.comはGithubに似ていますので、オプションを持つことができます。しかし、bitbucketは無料のプライベートレポジトリを許可しているので、予算上の理由からいいです。 – Kray

答えて

2

私の推測は閉じていますか?>あなたは空白とelseキーワードを出力します。これは、「ユーザー」に述べたコード上の意味

+0

そうではありませんが、何か似ています。それは本当に空白を出力しません。私はそれが単なる文法の問題だと思う。 – scarface

+0

はい、空白を出力します。 ?>と<?phpの間の空白(改行を含む)さえも出力されます – svn

1

を作成しない、あなたはセッション変数の値を設定しなかった

if (condition) { 
    // do stuff 
} 
print ' '; 
else { .... 

と同様です。この理由から、ページには常にユーザーログインページの一部が表示されます。正しいコードは以下のとおりです。コードはこのような状況のために変更ところ

<?php 
ob_start(); 
session_start(); 

if(isset($_REQUEST["submit"]) === true) { 
    $_SESSION['user'] = trim($_REQUEST["username"]); 
} 

?> 
<!doctype html> 
<html> 
    <head> 
    <title>Example</title> 
    </head> 
    <body> 

    <?php 
     if(! isset($_SESSION['user'])) 
     { 
    ?> 
     <main> 
      <div class="container"> 
       <form class="login" method="post" action=""> 
        <input type="text" placeholder="Username" name="username"/><br> 
        <input type="text" placeholder="Password" name="password"/><br> 
        <?php 
         if(isset($_SESSION['error'])) 
          echo $error; 
        ?> 
        <input type="submit" value="Login" name="submit"/><br> 
       </form> 
      </div> 
     </main> 
    <?php } 
     else 
      { 
       unset($_SESSION['error']); 
    ?> 
     <header> 
      <div class="container"> 
       <h1>PHP Quizzer</h1> 
      </div> 
     </header> 

     <main> 
      <div class="container"> 
       <h2>This is a PHP quizzer.</h2> 
       <p>This is a multiple choice quiz to test your knowledge of PHP</p> 
       <ul> 
        <li><strong>Number of Questions: </strong></li> 
        <li><strong>Type: </strong>Multiple Choice</li> 
        <li><strong>Estimated Time: </strong></li> 
       </ul> 
       <a href="questions.php?n=1" class="start">Start Quiz</a> 
      </div> 
     </main> 
    <?php } ?> 


    </body> 
</html> 
関連する問題