Thinkphp3.2.3如何檢驗驗證碼?

來源:互聯網
上載者:User
關鍵字 php

用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("未輸入驗證碼");      }    }  }

前台代碼如下:

    

輸入後提交,顯示如下:

6436object(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("未輸入驗證碼");      }    }  }

前台代碼如下:

    

輸入後提交,顯示如下:

6436object(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 "驗證碼正確!!";}    }  }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.