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

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

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools