当前位置:Gxlcms > PHP教程 > Thinkphp3.2.3如何检验验证码?

Thinkphp3.2.3如何检验验证码?

时间:2021-07-01 10:21:17 帮助过:33人阅读

用TP3.2框架在Verify控制器中生成验证码代码如下:

  public function verify_c()
  {
    $Verify = new \Think\Verify();
    $Verify->fontSize = 18;
    $Verify->length = 4;
    $Verify->useNoise = false;
    $Verify->codeSet = '0123456789';
    $Verify->imageW = 130;
    $Verify->imageH = 50;
    //$Verify->expire = 600;
    $Verify->entry();
  }

在Verify控制器中输出验证码如下:

  public function pv()
  {
    if (!empty($_POST)) {
      $verify = new \Think\Verify();
      $code = $_POST['code'];
      if (!empty($code)) {
        echo($code . "\n");
//      dump($code);
        if (!empty($verify)) {
          echo($verify);
          var_dump($verify);
        } else {
          echo("验证码为空");
        }
      } else {
        echo("未输入验证码");
      }
    }
  }

前台代码如下:

    

输入后提交,显示如下:

6436

object(Think\Verify)[6]
  protected 'config' => 
    array (size=15)
      'seKey' => string 'ThinkPHP.CN' (length=11)
      'codeSet' => string '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY' (length=52)
      'expire' => int 1800
      'useZh' => boolean false
      'zhSet' => string '们以我到他会作时要动国产的一是工就年阶义... (length=4500)
      'useImgBg' => boolean false
      'fontSize' => int 25
      'useCurve' => boolean true
      'useNoise' => boolean true
      'imageH' => int 0
      'imageW' => int 0
      'length' => int 5
      'fontttf' => string '' (length=0)
      'bg' => 
        array (size=3)
          0 => int 243
          1 => int 251
          2 => int 254
      'reset' => boolean true
  private '_image' => null
  private '_color' => null

为什么取不到TP生成的验证码呢?

回复内容:

用TP3.2框架在Verify控制器中生成验证码代码如下:

  public function verify_c()
  {
    $Verify = new \Think\Verify();
    $Verify->fontSize = 18;
    $Verify->length = 4;
    $Verify->useNoise = false;
    $Verify->codeSet = '0123456789';
    $Verify->imageW = 130;
    $Verify->imageH = 50;
    //$Verify->expire = 600;
    $Verify->entry();
  }

在Verify控制器中输出验证码如下:

  public function pv()
  {
    if (!empty($_POST)) {
      $verify = new \Think\Verify();
      $code = $_POST['code'];
      if (!empty($code)) {
        echo($code . "\n");
//      dump($code);
        if (!empty($verify)) {
          echo($verify);
          var_dump($verify);
        } else {
          echo("验证码为空");
        }
      } else {
        echo("未输入验证码");
      }
    }
  }

前台代码如下:

    

输入后提交,显示如下:

6436

object(Think\Verify)[6]
  protected 'config' => 
    array (size=15)
      'seKey' => string 'ThinkPHP.CN' (length=11)
      'codeSet' => string '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY' (length=52)
      'expire' => int 1800
      'useZh' => boolean false
      'zhSet' => string '们以我到他会作时要动国产的一是工就年阶义... (length=4500)
      'useImgBg' => boolean false
      'fontSize' => int 25
      'useCurve' => boolean true
      'useNoise' => boolean true
      'imageH' => int 0
      'imageW' => int 0
      'length' => int 5
      'fontttf' => string '' (length=0)
      'bg' => 
        array (size=3)
          0 => int 243
          1 => int 251
          2 => int 254
      'reset' => boolean true
  private '_image' => null
  private '_color' => null

为什么取不到TP生成的验证码呢?

不用你取得代码,直接调用工具类中的check就可以验证了

$verify = new ThinkVerify();
return $verify->check($code);

自动完成中这样使用就更好了:

    /**
     * 验证验证码.
     * @param string $code 验证码.
     * @return bool
     */
    protected function checkCode($code) {
        $verify = new \Think\Verify();
        return $verify->check($code);
    }

ob_clean();//在初始化Verify前加上这个试试
$verify = new ThinkVerify();

  public function verify_c()
  {
    ob_clean();
    $Verify = new \Think\Verify();
    $Verify->fontSize = 18;
    $Verify->length = 4;
    $Verify->useNoise = false;
    $Verify->codeSet = '0123456789';
    $Verify->imageW = 130;
    $Verify->imageH = 50;
    //$Verify->expire = 600;
    $Verify->entry();
  }

我自己解决了可以用以下来直接验证

  public function pv()
  {
    if (!empty($_POST)) {
      $verify = new \Think\Verify();
      $code = $_POST['code'];
      if(!$verify->check($code))
      {
        $this->error("验证码错误!");
        return;
      }else{
      echo "验证码正确!!";}
    }
  }

人气教程排行