在php中,如何在提交时将输入字段与变量进行比较?

在php中,如何在提交时将输入字段与变量进行比较?,php,php-7,Php,Php 7,我创建了一个随机字符串,并将其用作验证码,但无法验证。我对PHP很在行。这里我使用了isset($\u POST['submit'])但是没有点击提交,这个表单显示错误(变量)(需要strCaptcha)。我的逻辑是,在提交时,单击“代码”将“strCaptcha”的值与$str进行比较,并显示错误或运行表单 $error = ''; if (isset($_POST['submit'])){ if (empty($_POST["strCaptcha"]) || $_POST["strCap

我创建了一个随机字符串,并将其用作验证码,但无法验证。我对PHP很在行。这里我使用了isset($\u 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>
$error='';
如果(isset($_POST['submit'])){
如果(空($_POST[“strCaptcha”])|$_POST[“strCaptcha”]!=$str){
$error=“strCaptcha是必需的”;
}
}

如果您在html的隐藏字段中以纯文本形式提供captcha值,那么脚本很容易被欺骗

您必须创建随机字符串并将值保存到会话中,通过这样做,值不会向用户公开,您可以稍后使用它进行比较

页面代码

<?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);



您确定显示的随机字符串与表单提交时得到的随机字符串相同吗?我看不到图像的路径,也看不到将captcha值添加到$str变量的位置….@davidmpaz随机字符串仅在页面刷新时更改,因此在提交时如何将我的输入值与$str进行比较“但是没有点击提交,这个表单显示了错误”-什么错误?请具体说明,如果您谈论的是PHP本身发布的消息,请逐字引用-如果您只说“正在显示错误”,这里没有人知道您在说什么。您是否启用了正确的PHP错误报告功能?如果没有,请首先执行该操作。并且$action和$str在第一个代码块中应该来自何处,目前还不清楚。问题是,这是WordPress模板,因此我无法在此启动会话。我在帖子中没有看到这是word press…如果您正在编写PHP code您可以启动会话。没有问题,有没有办法让我在单个页面上执行此操作?您将如何创建映像?如果您使用JavaScript库,则无法达到验证码的目的。找到一种在服务器端创建映像的方法,您的问题就解决了。
<?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>
<?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);