【发布时间】:2021-03-04 17:54:40
【问题描述】:
我正在制作一个测验/madlibs 类型的网站,所以除了验证之外一切正常。每当用户输入特殊字符或将输入字段留空时,都会出现警告,并且不应允许提交按钮显示最终结果。
现在当我输入例如“?”然后按下提交按钮,它将显示包含所有答案的最终结果。
我的验证做错了什么?我怎样才能做到这一点,这样你就不能把它留空并使用特殊字符。
我用这段代码检查验证:
<?php
$Input1 = $Input2 = $Input3 = $Input4 = $Input5 = $Input6 = $Input7 = "";
$Input1Err = $Input2Err = $Input3Err = $Input4Err = $Input5Err = $Input6Err = $Input7Err = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["Input1"])){
$Input1Err = "enter something.";
}else{
$Input1 = test_input($_POST["Input1"]);
if(!preg_match("/[^a-z0-9 _]+$/i", $Input1)){
$Input1Err = "only letters and white space allowed";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
} ?>
对于每个输入,我都进行了这样的验证:
if(empty($_POST["Input1"])){
$Input1Err = "enter something.";
}else{
$Input1 = test_input($_POST["Input1"]);
if(!preg_match("/[^a-z0-9 _]+$/i", $Input1)){
$Input1Err = "only letters and white space allowed";
}
}
这里是完整的html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Mad Libs</title>
<link rel="stylesheet" type="text/css" href="Site.css">
</head>
<body>
<h1 class="text">Quiz game</h1>
<div class="container">
<nav>
<ul>
<li><a href="Quiz.php">quiz</a></li>
</ul>
</nav>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<h1> quiz</h1>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$Input1 = $_POST["Input1"];
$Input2 = $_POST["Input2"];
$Input3 = $_POST["Input3"];
$Input4 = $_POST["Input4"];
$Input5 = $_POST["Input5"];
$Input6 = $_POST["Input6"];
$Input7 = $_POST["Input7"];
echo "Driving a car can be fun if you follow this $Input1. advice:
When approaching a $Input2 on the right, always blow your $Input4 Before making a
$Input3 turn, always stick your $Input2 out of the window.
Every 2000 miles, have your $Input1.inspected and your $Input5 checked.
When approaching a school, watch out for $Input6.
Above all, drive $Input6 the $Input7 you save may be your own!$Input2";
?>
<?php }else{ ?>
<p>Question.. <input type="text" name="Input1" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input2" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input3" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input4" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input5" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input6" placeholder="enter here"></p>
<p>Question.. <input type="text" name="Input7" placeholder="enter here"></p>
<input type="submit" name="submit" value="Send">
</form>
<?php } ?>
<footer>
<p>@2021.</p>
</footer>
</div>
</body>
</html>
【问题讨论】:
-
那么在哪里你实际上是在执行这个验证?您显示为“完整 html”的代码包含再次提交到相同 URL 的表单 - 那么验证代码在哪里发挥作用呢?
-
我正要说同样的话——验证码似乎与它的其余部分无关。您显示的“完整”HTML+PHP 不包含它,或者根本不调用它。
-
html 中的 php 块用于显示最终结果,html 之外的 php 用于验证输入。通过表单操作,我使用该验证对吗?
-
使用该表单操作,您会将提交发送到最初请求此文档的相同 URL。如果 那个 脚本不包含您的验证代码……那么验证永远不会发生。
-
您的表单只是回发到加载它的同一个 URL - 即同一个 PHP 脚本。目前尚不清楚验证码的实际位置。它与 HTML 在同一个 PHP 文件中吗?请解释上下文。你已经在你的问题中展示了它,就好像它是分开的一样。即使它是都在同一个文件中,它所做的只是填充一个“错误”变量。其余代码从未真正检查或显示它或使用它来决定要做什么。