当前位置: 首页 > 知识库问答 >
问题:

PHP:验证码验证码不能正常工作;

琴光亮
2023-03-14

我正在创建一个非常简单的验证码验证过程。

  <?php session_start();

   include 'database_connect.php';

   function getRandomString($length)  {
   $validCharacters =  
   "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
    $validCharNumber = strlen($validCharacters);
    $result = "";

    for ($i = 0; $i < $length; $i++) {
    $index = mt_rand(0, $validCharNumber - 1);
    $result .= $validCharacters[$index];
    }
    return $result; }

    $captcha_value = getRandomString(6);

    ?>

   <!DOCTYPE html>
   <html>
   <head>
        <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
        <META HTTP-EQUIV="Cache-Control" CONTENT="no-store">
        <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
        <META HTTP-EQUIV="Expires" CONTENT="0">
        <META CHARSET="UTF-8">      
        <title>Captcha Test</title>         
  </head>
 <body>
    <fieldset><legend>CAPTCHA :</legend>

     <?php 
            if (isset($_POST['submit'])) {

                $post_captcha = $_POST['captcha'];

                if ($post_captcha == $captcha_value) {

                $clear_failed_logins =  mysqli_query($conn,("Delete FROM 
                                        login_attempts where login = 
                                        '$_POST[login]'")) 
                                        or die(mysqli_error($conn)); 

                header ("Location: /example.com/login.php");    
                exit();     
            }
            else {

            echo "<p style='color:red; font-weight:bold;'>The value you  
                   entered is not correct! Please try again.</p>";
             }
          }
        ?> 

        <p>Please input the characters you see into the text-box below :</p>
        <p>  <?= $captcha_value ?> </p>
        <form method="POST" action="captcha.php">
        <input type="text" name="captcha" id="captcha" size=10 
                     autocomplete="off" required><br><br><br> 
        <input type="submit" name="submit" id="submit" value="SUBMIT"> 
        </form>
    </fieldset> 

共有1个答案

刘才俊
2023-03-14

在php中,在向用户输出某些内容后,不能用header重定向,所以在输出某些内容之前,将if else块移到页面的顶部。如果要在此位置重定向,请尝试元刷新

 <meta http-equiv="refresh" content="0; URL=http://www.domain.com">

另外,您应该在表单中存储captcha值,因为它在每次刷新页面时都会重新生成(因此在表单提交后),我建议您使用reCaptcha进行尝试,这很容易实现,而且更加安全

 类似资料:
  • 接口说明 验证验证码 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 如开启https功能,请求地址的协议应改为https,如:https://www.example.com/wish3dearth/api/access/v1.0.0/getLicenseInfo API地址 GET /authcenter/api/verify/v1.0.0/check

  • 本文向大家介绍PHP实现验证码校验功能,包括了PHP实现验证码校验功能的使用技巧和注意事项,需要的朋友参考一下 验证码的校验是利用PHP中的 SESSION功能来实现。 在最顶端声明函数 session_start(); 告诉服务器我们要用这个函数的功能。 接下来我们用到的就是验证码实现的代码。这里用英文数字的代码为例。 然后在验证码实现之前声明一个空变量,用来存放验证码。 用 POST 方式来接

  • Captcha中间件提供了验证码服务,他是一个 Tango 的插件。 安装 go get github.com/tango-contrib/captcha 示例 package main import ( "github.com/lunny/tango" "github.com/tango-contrib/captcha" "github.com/tango-contrib

  • 发送验证码支持 email 和 phone 非注册用户发送: POST /verifycodes/register 注册用户发送: POST /verifycodes 输入 名称 类型 描述 phone String 如果 email 不存在则必须,以 sms 模式给手机发送验证码。 email String 如果 phone 不存在则必须,以 mail 模式给邮箱发送验证码。 响应 Stat

  • 验证码验证 cmf_captcha_check($value, $id = "") 参数 $value:用户提交的验证码 $id:前台设置的验证码 id,默认为空,用户在前台只能定义整形的 id,最大为5 控制器使用 $captcha = $request->param('captcha'); if (cmf_captcha_check($captcha)) { echo "验证码正确"

  • 1.生成验证码 生成验证码的函数: sp_verifycode_img($imgparam,$imgattrs); 参数: $imgparam:控制验证码的样式,(默认值length=4&font_size=20&width=238&height=50&use_curve=1&use_noise=1) $imgattrs:生成的验证码img标签的原生属性,除src,onclick之外都可以设置,