search
Homephp教程php手册php验证码实现局部刷新

php验证码实现局部刷新

Jun 13, 2016 am 10:12 AM
phpgenerallyrefreshaccomplishlocalusyesofcodethisverify

一般在用验证码时我们都要实现局部刷新了,这个是必然的,也是提高用户体验的一点,下面我们来看一个php验证码在实际应用中的用法,有需要的朋友来参考一下。

/*
 * Auth_code()是验证码函数
 * @access public
 * @param int $width 表示验证码的长度,默认为80
 * @param int $height 表示验证码的高度,默认为20
 * @param int $num  表示验证码中数字位数,默认为4
 * @param int $line  表示验证码中线的条数,默认为4
 * @param int $line  表示验证码中点的个数,默认为150
 */

 代码如下 复制代码

function Auth_code($width = 80,$height = 20,$num = 4,$line = 4,$dot = 150) {

 $length = floor($width/$num);  //floor:取整数部分 length:每段平均长度
 for($i=0;$i   @$rand.=dechex(mt_rand(1,15));
 $_SESSION['code']=$rand;
 
 //新建一个黑色底的画板
 $im=imagecreatetruecolor($width,$height);
 
 //取色
 $green=imagecolorallocate($im,0,255,0);
 $red = imagecolorallocate($im,255,0,0);
 $white=imagecolorallocate($im,255,255,255);
 $black = imagecolorallocate($im,0,0,0);
 
 //填充画板
 //imagefill($im,0,0,$green);
   
 //画线
 for($i=0;$i   $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imageline($im,rand(10,$width-10),0,rand(0,$width),$height,$color);
 }
 //画点
 for($i=0;$i   $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imagesetpixel($im,rand(0,$width),rand(0,$height),$color);
 }
 //画数字
 for($i=0;$i   imagestring($im,5,mt_rand($i*$length+1,($i+1)*$length-8),mt_rand(0,$height-14),$_SESSION['code'][$i],$white);
 }
 
 //输出图片
 header("content-type:image/jpeg");
 imagejpeg($im);
}

 -主要函数:
       imagecreatetruecolor  //新建一个黑色底的画板
       imagecolorallocate     //取色
       imagefill                     //填充画板
       imageline                  //画线
       imagesetpixel           //画点
       imagestring             //画字符串(本验证码为数字)
       注意:在将数字验证码画上画板时要注意字体越界,所以要相应减去一些范围,详见附件。
    -局部刷新验证码:

 代码如下 复制代码
       php验证码实现局部刷新
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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