错误计数 - PHP
Error Count - PHP
我有一个以电子邮件作为字段名称的表单。我想要做的是,如果电子邮件不等于 $emailToCheck
不等于 $_POST['email']
,它应该在第一次时抛出错误。第二次,如果用户再次输入错误的电子邮件 ID,即使页面刷新,它也应该始终重定向到 "error.htm"
。
即使电子邮件 ID 输入错误两次,表单始终显示也不起作用。
<?php
if (!empty($_POST['email'])) {
$email="website@test.com";
if($email!=$_POST['email'])
{
$count="1";
}
if($count=="2"){
header("Location: /error.htm");
exit(0);
}
}
if($count!="2"){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form</title>
</head>
<body id="main_body" >
<div id="form_container">
<h1><a>Form</a></h1>
<form id="form_1123848" class="appnitro" method="post" action="">
<div class="form_description">
<h2>Form</h2>
<p> <input type="text" name="email" value="" /></p>
</div>
<ul >
<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
</body>
</html>
<?
}
?>
表单提交后,页面会重新加载,计数器会重置。为了实际计数,您需要在表单中提供该值,并在提交表单时将其传递给 PHP。
<?php
// Try to get the amount of attempts from the POSTed data
$count = isset($_POST['count']) ? $_POST['count'] : 0;
if (isset($_POST['email'])) {
$email = "website@test.com";
if ($email != $_POST['email']) {
$count++;
}
if ($count == 2) {
header("Location: /error.htm");
}
}
if ($count <= 2):
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form</title>
</head>
<body id="main_body" >
<div id="form_container">
<h1><a>Form</a></h1>
<form id="form_1123848" class="appnitro" method="post" action="">
<!-- Let the POST data know this is the x attempt -->
<input type="hidden" name="count" value="<?php echo $count; ?>">
<div class="form_description">
<h2>Form</h2>
<p> <input type="text" name="email" value="" /></p>
</div>
<ul>
<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
</div>
</body>
</html>
<?php endif; ?>
此外,您的编码风格远非一致。努力解决这个问题!
这里有两个问题:
1. 您将 $count
定义为字符串,并且从不递增它。如果您查看您的代码,每次不匹配时 $count
都会被专门设置为 1
。应该如何到达 2
?
2. 此外,这里的数据是无状态的。脚本应该如何知道 $count
在上一次调用中设置的值?您还需要将 $count
设置为会话变量,以便脚本知道其先前的值。
您应该尝试将您的代码更新为与此类似的内容:
// Check if `email` passed in POST request:
if ($_POST['email']) {
$email = "website@test.com"; //Manually define expected email address.
// Check if provided email does *not* match the expected email:
if ($email !== $_POST['email']) {
// Record the mismatch attempt in session and increment:
if (!($_SESSION['incorrectEmailCount'])) {
// If this is the first mismatch, define the session variable, and set to 1.
$_SESSION['incorrectEmailCount'] = 1;
} else {
// Session variable already set due to previous mismatch. Increment it.
$_SESSION['incorrectEmailCount']++;
}
}
// If user entered incorrect email more than once:
if ($_SESSION['incorrectEmailCount'] > 1) {
// Redirect to error page and stop execution.
header("Location: /error.htm");
exit(0);
}
}
我有一个以电子邮件作为字段名称的表单。我想要做的是,如果电子邮件不等于 $emailToCheck
不等于 $_POST['email']
,它应该在第一次时抛出错误。第二次,如果用户再次输入错误的电子邮件 ID,即使页面刷新,它也应该始终重定向到 "error.htm"
。
即使电子邮件 ID 输入错误两次,表单始终显示也不起作用。
<?php
if (!empty($_POST['email'])) {
$email="website@test.com";
if($email!=$_POST['email'])
{
$count="1";
}
if($count=="2"){
header("Location: /error.htm");
exit(0);
}
}
if($count!="2"){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form</title>
</head>
<body id="main_body" >
<div id="form_container">
<h1><a>Form</a></h1>
<form id="form_1123848" class="appnitro" method="post" action="">
<div class="form_description">
<h2>Form</h2>
<p> <input type="text" name="email" value="" /></p>
</div>
<ul >
<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
</body>
</html>
<?
}
?>
表单提交后,页面会重新加载,计数器会重置。为了实际计数,您需要在表单中提供该值,并在提交表单时将其传递给 PHP。
<?php
// Try to get the amount of attempts from the POSTed data
$count = isset($_POST['count']) ? $_POST['count'] : 0;
if (isset($_POST['email'])) {
$email = "website@test.com";
if ($email != $_POST['email']) {
$count++;
}
if ($count == 2) {
header("Location: /error.htm");
}
}
if ($count <= 2):
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form</title>
</head>
<body id="main_body" >
<div id="form_container">
<h1><a>Form</a></h1>
<form id="form_1123848" class="appnitro" method="post" action="">
<!-- Let the POST data know this is the x attempt -->
<input type="hidden" name="count" value="<?php echo $count; ?>">
<div class="form_description">
<h2>Form</h2>
<p> <input type="text" name="email" value="" /></p>
</div>
<ul>
<li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
</div>
</body>
</html>
<?php endif; ?>
此外,您的编码风格远非一致。努力解决这个问题!
这里有两个问题:
1. 您将 $count
定义为字符串,并且从不递增它。如果您查看您的代码,每次不匹配时 $count
都会被专门设置为 1
。应该如何到达 2
?
2. 此外,这里的数据是无状态的。脚本应该如何知道 $count
在上一次调用中设置的值?您还需要将 $count
设置为会话变量,以便脚本知道其先前的值。
您应该尝试将您的代码更新为与此类似的内容:
// Check if `email` passed in POST request:
if ($_POST['email']) {
$email = "website@test.com"; //Manually define expected email address.
// Check if provided email does *not* match the expected email:
if ($email !== $_POST['email']) {
// Record the mismatch attempt in session and increment:
if (!($_SESSION['incorrectEmailCount'])) {
// If this is the first mismatch, define the session variable, and set to 1.
$_SESSION['incorrectEmailCount'] = 1;
} else {
// Session variable already set due to previous mismatch. Increment it.
$_SESSION['incorrectEmailCount']++;
}
}
// If user entered incorrect email more than once:
if ($_SESSION['incorrectEmailCount'] > 1) {
// Redirect to error page and stop execution.
header("Location: /error.htm");
exit(0);
}
}