如何在 php 中将输入字段与提交时的变量进行比较?
How to compare input field with variable on submit in php?
我创建了一个随机字符串并将其用作验证码但无法验证。我在 PHP 中非常菜鸟。在这里我使用 isset($_POST['submit'])
但是没有点击提交这个表单显示错误(变量)(strCaptcha 是必需的)。我的逻辑是,在提交时,点击代码会将 'strCaptcha' 的值与 $str
进行比较,并显示错误或 运行 表单。
$error = '';
if (isset($_POST['submit'])){
if (empty($_POST["strCaptcha"]) || $_POST["strCaptcha"] != $str) {
$error = "strCaptcha is required";
}
}
<form method="post" action="<?php echo $action; ?>" >
<p><label><b>COA Number:</b></label><br>
<input type="text" name="number" class="text"><br><be>
<span id="realcap" style="visibility:hidden;"><?php echo implode(' ',str_split($str)); ?></span><be>
<span style="color:red;"><?php echo $error; ?></span><br>
<img src="" id="captch" alt="This Is a CAPTCHA Image"><br>
<label><b>Enter the text of the image above:</b></label><br>
<input name="strCaptcha" type="text" class="text" value="" maxlength="5"><br><br>
<input type="submit" class="awesome medium" name="submit" value="Verify Now"></p>
</form>
添加图片
<script src="//cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script>
html2canvas(document.getElementById("realcap"), {
onrendered: function(canvas) {
var screenshot = canvas.toDataURL("image/png");
document.getElementById("captch").setAttribute("src", screenshot);
}
});
</script>
随机字符串
$n=5;
function getName($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
$str = getName($n);
如果您在 html.
的隐藏字段中以纯文本形式提供验证码值,那么您的脚本很容易被欺骗
您必须创建随机字符串并将值保存到会话中,这样该值就不会暴露给用户,您可以稍后使用它进行比较。
页面代码
<?php
session_start();
$captcha = $_SESSION['captcha'];
$error = '';
if (isset($_POST['submit'])){
if (empty($_POST["strCaptcha"]) || $_POST["strCaptcha"] != $captcha) {
$error = "strCaptcha is required";
}
}
<form method="post" action="<?php echo $action; ?>" >
<p><label><b>COA Number:</b></label><br>
<input type="text" name="number" class="text"><br>
<span style="color:red;"><?php echo $error; ?></span><br>
<img src="image.php" id="captch" alt="This Is a CAPTCHA Image"><br>
<label><b>Enter the text of the image above:</b></label><br>
<input name="strCaptcha" type="text" class="text" value="" maxlength="5"><br><br>
<input type="submit" class="awesome medium" name="submit" value="Verify Now"></p>
</form>
图片必须在服务器端生成。
示例未经测试。 image.php
<?php
$n=5;
function getName($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
$str = getName($n);
session_start();
$_SESSION['captcha'] = $str;
// Generate image using the $str to create the image.
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, $str, $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
我创建了一个随机字符串并将其用作验证码但无法验证。我在 PHP 中非常菜鸟。在这里我使用 isset($_POST['submit'])
但是没有点击提交这个表单显示错误(变量)(strCaptcha 是必需的)。我的逻辑是,在提交时,点击代码会将 'strCaptcha' 的值与 $str
进行比较,并显示错误或 运行 表单。
$error = '';
if (isset($_POST['submit'])){
if (empty($_POST["strCaptcha"]) || $_POST["strCaptcha"] != $str) {
$error = "strCaptcha is required";
}
}
<form method="post" action="<?php echo $action; ?>" >
<p><label><b>COA Number:</b></label><br>
<input type="text" name="number" class="text"><br><be>
<span id="realcap" style="visibility:hidden;"><?php echo implode(' ',str_split($str)); ?></span><be>
<span style="color:red;"><?php echo $error; ?></span><br>
<img src="" id="captch" alt="This Is a CAPTCHA Image"><br>
<label><b>Enter the text of the image above:</b></label><br>
<input name="strCaptcha" type="text" class="text" value="" maxlength="5"><br><br>
<input type="submit" class="awesome medium" name="submit" value="Verify Now"></p>
</form>
添加图片
<script src="//cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script>
html2canvas(document.getElementById("realcap"), {
onrendered: function(canvas) {
var screenshot = canvas.toDataURL("image/png");
document.getElementById("captch").setAttribute("src", screenshot);
}
});
</script>
随机字符串
$n=5;
function getName($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
$str = getName($n);
如果您在 html.
的隐藏字段中以纯文本形式提供验证码值,那么您的脚本很容易被欺骗您必须创建随机字符串并将值保存到会话中,这样该值就不会暴露给用户,您可以稍后使用它进行比较。
页面代码
<?php
session_start();
$captcha = $_SESSION['captcha'];
$error = '';
if (isset($_POST['submit'])){
if (empty($_POST["strCaptcha"]) || $_POST["strCaptcha"] != $captcha) {
$error = "strCaptcha is required";
}
}
<form method="post" action="<?php echo $action; ?>" >
<p><label><b>COA Number:</b></label><br>
<input type="text" name="number" class="text"><br>
<span style="color:red;"><?php echo $error; ?></span><br>
<img src="image.php" id="captch" alt="This Is a CAPTCHA Image"><br>
<label><b>Enter the text of the image above:</b></label><br>
<input name="strCaptcha" type="text" class="text" value="" maxlength="5"><br><br>
<input type="submit" class="awesome medium" name="submit" value="Verify Now"></p>
</form>
图片必须在服务器端生成。
示例未经测试。 image.php
<?php
$n=5;
function getName($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
$str = getName($n);
session_start();
$_SESSION['captcha'] = $str;
// Generate image using the $str to create the image.
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, $str, $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);