search
HomeBackend DevelopmentPHP Tutorialthinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial

thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code

Before introducing the use of thinkphp3.2 verification code, I will introduce the ThinkPHP verification code in detail. The specific content is as follows

ThinkPHP has built-in support for verification codes and can be used directly. To use the verification code, you need to import the ORG.Util.Image class library and ORG.Util.String class library in the extended class library.
Verification code method
We add a verify method to the module class to display the verification code. The simplest example:

Public function verify(){
  // 导入Image类库
  import("ORG.Util.Image");
  Image::buildImageVerify();
}

The import method is ThinkPHP’s built-in class library and file import method. The file imported in the above example is the Lib/ORG/Util/Image.class.php file in the ThinkPHP system directory. If the Image class library has been copied to the current project, such as Lib/ORG, you can use:

import("@.Util.Image");

The import method is ThinkPHP’s built-in class library and file import method. The file imported in the above example is the Lib/ORG/Util/Image.class.php file in the ThinkPHP system directory.
Access verification code
You can access the verification code method directly in the browser to determine whether the verification code can be displayed normally:
http://127.0.0.1/index.php/Public/verify
If everything goes well, the verification code will appear as follows:

Use verification code in form
To use the verification code in the form page, it is called with the html img tag:

<input type="text" name="verify">
<img  src="/static/imghwm/default1.png"  data-src="-Article-verify"  class="lazy"  id="verifyImg" onClick="changeVerify()" title="点击刷新thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial" / alt="thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial" >

The src attribute value is the verification code method access address, which varies depending on the actual situation.
Verification code refresh
When the verification code image is clicked, the JavaScript changeVerify() function is triggered to re-read the verification code, thereby refreshing the verification code. The function reference is as follows:

<script language="JavaScript">
function changeVerify(){
 var timenow = new Date().getTime();
 document.getElementById('verifyImg').src='-Article/verify/'+timenow; 
}
</script>

Verification code verification
When calling the verification code verify, buildImageVerify will record the MD5 information of this verification code. In the form verification operation, use the following method to check whether the verification code is correct:

if($_SESSION['verify'] != md5($_POST['verify'])) {
  $this->error('thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial错误!');
}

The verify name in $_SESSION['verify'] is the default SESSION registration name of the buildImageVerify method. For details, see the buildImageVerify syntax.
The above example demonstrates the simplest way to use ThinkPHP verification code. The verification code in the above example is 4 digits. If you want to use more styles of verification codes and Chinese verification codes, see the rest of this section: "ThinkPHP uses different styles and Chinese verification codes".
The reason why the verification code is not displayed
It is found that the verification code cannot be displayed. The possible reasons are as follows:
1. Whether PHP has installed GD library support.
2. Is there any output before output (especially UTF8 BOM header information output).
3. Whether the Image class library is imported correctly.
4. If it is a form page, please check whether the verification code display method is correctly called.

The following will introduce to you how to generate the verification code and click to refresh the verification code in thinkphp3.2. The specific content is as follows

1. Instantiate the class that generates the verification code (this method is placed in IndexController for easy access)

/** 
 * 
 * thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial生成 
 */ 
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(); 
} 

2. The src attribute of the image that the front desk needs to generate a verification code points to

<p class="top15 captcha" id="captcha-container"> 
 <input name="verify" width="50%" height="50" class="captcha-text" placeholder="thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial" type="text">         
 <img class="left15 lazy"  src="/static/imghwm/default1.png"  data-src="{:U('Home/Index/verify_c',array())}"     style="max-width:90%"  style="max-width:90%" alt="thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial"Home/Index/verify_c',array())}" title="点击刷新"> 
</p> 

3. After writing the above, the verification code for page initialization will appear. What is written next is to refresh the new verification code image after clicking on the verification code image (this is done by modifying the src attribute of the image through jquery. The request processing function is the same, except that a random number is added after the request to distinguish the request for the previous picture)

// thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial生成 
var captcha_img = $('#captcha-container').find('img') 
var verifyimg = captcha_img.attr("src"); 
captcha_img.attr('title', '点击刷新'); 
captcha_img.click(function(){ 
  if( verifyimg.indexOf('&#63;')>0){ 
    $(this).attr("src", verifyimg+'&random='+Math.random()); 
  }else{ 
    $(this).attr("src", verifyimg.replace(/\&#63;.*$/,'')+'&#63;'+Math.random()); 
  } 
}); 

4. Verify whether the verification code is entered correctly
a. Add a global function to function.php in the common directory

/** 
 * thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial检查 
 */ 
function check_verify($code, $id = ""){ 
  $verify = new \Think\Verify(); 
  return $verify->check($code, $id); 
} 

b. Add the check code in the processing method corresponding to the controller of the form submission

// 检查thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial 
$verify = I('param.verify',''); 
if(!check_verify($verify)){ 
  $this->error("亲,thinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code_PHP tutorial输错了哦!",$this->site_url,9); 
} 

Now you can use the tp3.2 verification code.
Supplement: When I was writing, I put the fourth step b into an ajax for verification and returned a test result. Then it is determined whether to submit the form based on the returned results. However, after the verification code passes the first verification, the second time will not work. I haven’t figured out the reason yet.

This is the entire content of this article. There is a small question at the end of the article. I hope you can come up with a solution. I also hope this article will be helpful to your study.

Articles you may be interested in:

  • A concise tutorial on how to use ThinkPHP verification code
  • Solutions for thinkphp verification code not showing up
  • ThinkPHP verification code and Pagination example tutorial
  • ThinkPHP implementation of file upload function with verification code example
  • Perfect solution to the problem that thinkphp verification code error cannot be displayed
  • Solution to the problem that ThinkPHP opens the verification code page and displays garbled characters Method
  • Simple method of using verification code in thinkPHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1100918.htmlTechArticlethinkphp3.2 click refresh to generate verification code, thinkphp3.2 verification code and then introduce how to use thinkphp3.2 verification code Before, let me introduce the ThinkPHP verification code in detail, the specific content is as follows T...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
thinkphp是不是国产框架thinkphp是不是国产框架Sep 26, 2022 pm 05:11 PM

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列Apr 20, 2022 pm 01:07 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了关于使用think-queue来实现普通队列和延迟队列的相关内容,think-queue是thinkphp官方提供的一个消息队列服务,下面一起来看一下,希望对大家有帮助。

thinkphp的mvc分别指什么thinkphp的mvc分别指什么Jun 21, 2022 am 11:11 AM

thinkphp基于的mvc分别是指:1、m是model的缩写,表示模型,用于数据处理;2、v是view的缩写,表示视图,由View类和模板文件组成;3、c是controller的缩写,表示控制器,用于逻辑处理。mvc设计模式是一种编程思想,是一种将应用程序的逻辑层和表现层进行分离的方法。

实例详解thinkphp6使用jwt认证实例详解thinkphp6使用jwt认证Jun 24, 2022 pm 12:57 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了使用jwt认证的问题,下面一起来看一下,希望对大家有帮助。

thinkphp扩展插件有哪些thinkphp扩展插件有哪些Jun 13, 2022 pm 05:45 PM

thinkphp扩展有:1、think-migration,是一种数据库迁移工具;2、think-orm,是一种ORM类库扩展;3、think-oracle,是一种Oracle驱动扩展;4、think-mongo,一种MongoDb扩展;5、think-soar,一种SQL语句优化扩展;6、porter,一种数据库管理工具;7、tp-jwt-auth,一个jwt身份验证扩展包。

一文教你ThinkPHP使用think-queue实现redis消息队列一文教你ThinkPHP使用think-queue实现redis消息队列Jun 28, 2022 pm 03:33 PM

本篇文章给大家带来了关于ThinkPHP的相关知识,其中主要整理了使用think-queue实现redis消息队列的相关问题,下面一起来看一下,希望对大家有帮助。

thinkphp 怎么查询库是否存在thinkphp 怎么查询库是否存在Dec 05, 2022 am 09:40 AM

thinkphp查询库是否存在的方法:1、打开相应的tp文件;2、通过“ $isTable=db()->query('SHOW TABLES LIKE '."'".$data['table_name']."'");if($isTable){...}else{...}”方式验证表是否存在即可。

thinkphp3.2怎么关闭调试模式thinkphp3.2怎么关闭调试模式Apr 25, 2022 am 10:13 AM

在thinkphp3.2中,可以利用define关闭调试模式,该标签用于变量和常量的定义,将入口文件中定义调试模式设为FALSE即可,语法为“define('APP_DEBUG', false);”;开启调试模式将参数值设置为true即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.