Table of Contents
thinkphp3.2点击刷新生成验证码,thinkphp3.2验证码
您可能感兴趣的文章:
Home php教程 php手册 thinkphp3.2点击刷新生成验证码,thinkphp3.2验证码

thinkphp3.2点击刷新生成验证码,thinkphp3.2验证码

Jun 13, 2016 am 08:45 AM
thinkphp Verification code

thinkphp3.2点击刷新生成验证码,thinkphp3.2验证码

再介绍thinkphp3.2验证码的使用方法之前,先为大家详细介绍ThinkPHP 验证码,具体内容如下

ThinkPHP 内置了验证码的支持,可以直接使用。要使用验证码,需要导入扩展类库中的 ORG.Util.Image 类库和 ORG.Util.String 类库。
验证码方法
我们通过在在模块类中增加一个 verify 方法来用于显示验证码,最简单的例子:

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

import 方法是 ThinkPHP 内置的类库和文件导入方法,上例导入的文件为 ThinkPHP 系统目录下 Lib/ORG/Util/Image.class.php 文件。如果已经将 Image 类库拷贝到了当前项目下,如 Lib/ORG 下,则可以以:

import("@.Util.Image");
Copy after login

import 方法是 ThinkPHP 内置的类库和文件导入方法,上例导入的文件为 ThinkPHP 系统目录下 Lib/ORG/Util/Image.class.php 文件。
访问验证码
可以直接在浏览器里访问该验证码方法以确定验证码是否能正常显示:
http://127.0.0.1/index.php/Public/verify
如果一切正常,显示验证码如下所示:

表单中使用验证码
在表单页面中使用验证码,是以 html img标签 来调用:

<input type="text" name="verify">
<img  src="/static/imghw/default1.png"  data-src="-Article-verify"  class="lazy"  id="verifyImg" onClick="changeVerify()" title="点击刷新验证码" / alt="thinkphp3.2点击刷新生成验证码,thinkphp3.2验证码" >
Copy after login

src 属性值即为验证码方法访问地址,视实际情况不同而不同。
验证码刷新
当点击验证码图片时,触发 JavaScript changeVerify() 函数重新读取验证码,从而实现验证码刷新。该函数参考如下:

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

验证码验证
在调用验证码 verify 的时候,buildImageVerify 会记录本次验证码的 MD5 信息。在表单验证操作里,以如下方法来检查验证码是否正确:

if($_SESSION['verify'] != md5($_POST['verify'])) {
  $this->error('验证码错误!');
}
Copy after login

其中 $_SESSION['verify'] 中的 verify 名称为 buildImageVerify 方法默认 SESSION 注册名称,具体见 buildImageVerify 语法。
上面例子演示了最简单的 ThinkPHP 验证码的使用方法。上面的例子验证码是 4 位数字,如果想使用更多风格的验证码以及中文验证码,参见本节其余部分内容:《ThinkPHP 使用不同风格及中文的验证码》。
验证码不显示原因
如下发现无法显示验证码,可能的原因如下:
1、PHP 是否已经安装 GD 库支持。
2、输出之前是否有任何的输出(尤其是 UTF8 的 BOM 头信息输出)。
3、Image 类库是否正确导入。
4、如果是表单页面,请查看是否正确调用了验证码显示方法。

下面就为大家介绍 thinkphp3.2 验证码生成和点击刷新验证码的实现方法,具体内容如下

一、实例化生成验证码的类(该方法放到IndexController里面便于访问)

/** 
 * 
 * 验证码生成 
 */ 
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(); 
} 
Copy after login

二、前台需要生成验证码的图片src属性指向

<p class="top15 captcha" id="captcha-container"> 
 <input name="verify" width="50%" height="50" class="captcha-text" placeholder="验证码" type="text">         
 <img class="left15 lazy"  src="/static/imghw/default1.png"  data-src="{:U('Home/Index/verify_c',array())}"     style="max-width:90%"  style="max-width:90%" alt="验证码"Home/Index/verify_c',array())}" title="点击刷新"> 
</p> 
Copy after login

三、写完上面的后,页面初始化的验证码就可以出现了,下面要写的就是点击验证码图片后,刷新出新的验证码图片(通过jquery修改图片的src属性来完成,请求的处理函数一样,只是在请求后加一个随机数,区别上一张图片的请求)

// 验证码生成 
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()); 
  } 
}); 
Copy after login

四、校验验证码输入是否正确
a.在common目录下的function.php里加入全局函数

/** 
 * 验证码检查 
 */ 
function check_verify($code, $id = ""){ 
  $verify = new \Think\Verify(); 
  return $verify->check($code, $id); 
} 
Copy after login

b.在表单提交的controller对应的处理方法里添加检查代码

// 检查验证码 
$verify = I('param.verify',''); 
if(!check_verify($verify)){ 
  $this->error("亲,验证码输错了哦!",$this->site_url,9); 
} 
Copy after login

到此tp3.2验证码的使用就可以了。
补充:我在写的时候将四的b步骤放到一个ajax里验证,返回一次检验结果。然后再依据返回结果确定是否要提交表单,但是在验证码通过第一次的校验后,第二次的就不可以了,目前还没想明白原因。

这就是本文的全部内容,文章最后还有一个小小的疑问,希望大家可以想出解决办法,也希望本文对大家的学习有所帮助。

您可能感兴趣的文章:

  • ThinkPHP验证码使用简明教程
  • thinkphp验证码显示不出来的解决方法
  • ThinkPHP验证码和分页实例教程
  • ThinkPHP实现带验证码的文件上传功能实例
  • 完美解决thinkphp验证码出错无法显示的方法
  • ThinkPHP打开验证码页面显示乱码的解决方法
  • thinkPHP中验证码的简单使用方法
Statement of this Website
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

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? Mar 13, 2024 pm 08:55 PM

What should I do if Google Chrome does not display the verification code image? Sometimes you need a verification code to log in to a web page using Google Chrome. Some users find that Google Chrome cannot display the content of the image properly when using image verification codes. What should be done? The editor below will introduce how to deal with the Google Chrome verification code not being displayed. I hope it will be helpful to everyone! Method introduction: 1. Enter the software, click the "More" button in the upper right corner, and select "Settings" in the option list below to enter. 2. After entering the new interface, click the "Privacy Settings and Security" option on the left. 3. Then click "Website Settings" on the right

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Can virtual numbers receive verification codes? Can virtual numbers receive verification codes? Jan 02, 2024 am 10:22 AM

The virtual number can receive the verification code. As long as the mobile phone number filled in during registration complies with the regulations and the mobile phone number can be connected normally, you can receive the SMS verification code. However, you need to be careful when using virtual mobile phone numbers. Some websites do not support virtual mobile phone number registration, so you need to choose a regular virtual mobile phone number service provider.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

See all articles