PHP显示某些<;span>;当PW或用户出错时
我希望PHP在输入错误的密码或用户名时显示一定的范围,否则它应该被隐藏。我怎样才能做到这一点?我尝试了所有的方法,但实际上我对PHP还是新手,并且在互联网上找到了这段代码 这是我的 这是我的代码:PHP显示某些<;span>;当PW或用户出错时,php,html,Php,Html,我希望PHP在输入错误的密码或用户名时显示一定的范围,否则它应该被隐藏。我怎样才能做到这一点?我尝试了所有的方法,但实际上我对PHP还是新手,并且在互联网上找到了这段代码 这是我的 这是我的代码: <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { session_start(); $username = $_POST['username']; $passwort = $_POST['passwort
<?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>
这个部门实际上应该来
当PW或用户出错时启动!
一种选择是简单地用“如果”环绕它
<?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; ?>
这个div实际上应该在PW或User出错时出现!
另外,您可能希望下次尝试PHP问题。如果替换
<span class="certainDiv" style="color:red;">This div should actually come up when PW or User is wrong!</span>
在第28行,将PHP标记中的其余代码替换为
} else {
$wrong = true;
}
它可以很好地处理这些更改:)您可以在凭据不正确时设置一个变量,并在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>
听起来不错,但不起作用!我刚刚上传了它,输入了错误的用户和密码,点击了登录,然后页面刷新,我没有被重定向,但是没有出现“certainDiv”.谢谢!现在效果很好!似乎是额外的代码,考虑到如果用户/通行证是正确的,它首先不会显示表单。参见标题('next.php');退出代码>if子句需要结束的第一个括号。我现在已经编辑了代码。
} else {
$wrong = true;
}
<?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>