当PW或用户出错时,PHP显示某些<span> </span>

时间:2014-02-19 00:05:05

标签: php html

我希望PHP在输入错误的密码或用户名时显示某个范围,否则应该隐藏它。我怎么能让这件事发生?我尝试了一切,但我实际上是PHP的新手,并在互联网上找到了这个代码。

这是我的jsfiddle

这是我的代码:

<?php
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
     session_start();

     $username = $_POST['username'];
     $passwort = $_POST['passwort'];

     $hostname = $_SERVER['HTTP_HOST'];
     $path = dirname($_SERVER['PHP_SELF']);

     // Benutzername und Passwort werden überprüft
     if ($username == 'admin' && $passwort == 'admin') {
        $_SESSION['angemeldet'] = true;

            // Weiterleitung zur geschützten Startseite
            if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {
                if (php_sapi_name() == 'cgi') {
                    header('Status: 303 See Other');
                }
                else {
                    header('HTTP/1.1 303 See Other');
                }
            }

       header('next.php');
       exit;
       }           
      }
?>

<div class="contactForm" id="contactForm" style="width: 500px; 
    margin: auto; float: left;">    
<form name="myform" method="post" action="login.php">
<p class="form" style="width: 245px; height: 116px; margin-right: 10px;">
   <input class="field" type="text" name="username" placeholder="Name" />
   <input class="field" type="password" autofocus name="passwort" 
       placeholder="Passwort" />
   <button class="button" name="Submit" >Login</button><br>
   <span class="certainDiv" style="color:red;">This div should actually come 
       up when PW or User is wrong!</span>
</p>
</form>
</div>

3 个答案:

答案 0 :(得分:0)

一种选择是简单地用'if'

包围它
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST'): ?>
    <span class="certainDiv" style="color:red;">This div should actually come up when PW or User is wrong!</span> 
<?php endif; ?>

此外,您可能希望下次尝试http://phpfiddle.org/以获取PHP问题。

答案 1 :(得分:0)

如果你更换

,很容易解决
<span class="certainDiv" style="color:red;">This div should actually come up when PW or User is wrong!</span>

使用

<?php
    if(isset($wrong)){
        echo '<span class="certainDiv" style="color:red;">This div should actually come up when PW or User is wrong!</span>';
    }
?>

然后在

exit;

在第28行,用

替换PHP标记中的其余代码
} else {
    $wrong = true;
}

这些变化应该可以正常工作:)

答案 2 :(得分:0)

您可以在凭据不正确时设置变量,并在html部分中使用它,如下所示。

<?php
 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
 session_start();

 $username = $_POST['username'];
 $passwort = $_POST['passwort'];

 $hostname = $_SERVER['HTTP_HOST'];
 $path = dirname($_SERVER['PHP_SELF']);

 // Benutzername und Passwort werden überprüft
 if ($username == 'admin' && $passwort == 'admin') {
    $_SESSION['angemeldet'] = true;

        // Weiterleitung zur geschützten Startseite
        if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {
            if (php_sapi_name() == 'cgi') {
                header('Status: 303 See Other');
            }
            else {
                header('HTTP/1.1 303 See Other');
            }
        }

      header('next.php');
      exit;
   } else {
      //means the username or password is incorrect at this point
      $invalidLogin = true;
   }

  }
?>


<div class="contactForm" id="contactForm" style="width: 500px; margin: auto; float: left;">    
<form name="myform" method="post" action="login.php">
<p class="form" style="width: 245px; height: 116px; margin-right: 10px;">
<input class="field" type="text" name="username" placeholder="Name" />
<input class="field" type="password" autofocus name="passwort" placeholder="Passwort" />
<button class="button" name="Submit" >Login</button><br>


<?php
   if(isset($invalidLogin) && ($invalidLogin)){
      echo '<span class="certainDiv" style="color:red;">This div should actually come up when PW or User is wrong!</span>';
   }
?>

</p>
</form>
</div>