Home  >  Article  >  Backend Development  >  PHP verification code implements partial refresh_PHP tutorial

PHP verification code implements partial refresh_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:10:25991browse

Generally when using verification codes, we have to implement partial refresh. This is inevitable and is also a point to improve user experience. Let’s take a look at the usage of PHP verification codes in practical applications. Friends in need can refer to it. one time.

/*
* Auth_code() is the verification code function
* @access public
* @param int $width represents the length of the verification code, the default is 80
* @param int $height represents the height of the verification code, the default is 20
* @param int $num represents the number of digits in the verification code, the default is 4
* @param int $line represents the number of lines in the verification code, the default is 4
* @param int $line represents the number of midpoints in the verification code, the default is 150
*/

//Get color
The code is as follows
 代码如下 复制代码

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

 $length = floor($width/$num);  //floor:取整数部分 length:每段平均长度
 for($i=0;$i<$num;$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<$line;$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<$dot;$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<$num;$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);
}

Copy code



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

$length = floor($width/$num); //floor: take the integer part length: the average length of each segment
for($i=0;$i<$num;$i++)
@$rand.=dechex(mt_rand(1,15));
$_SESSION['code']=$rand;

//Create a new drawing board with a black background
代码如下 复制代码

$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); //Fill the artboard //imagefill($im,0,0,$green); //Draw line for($i=0;$i<$line;$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);<🎜> }<🎜> //Draw dots<🎜> for($i=0;$i<$dot;$i++){<🎜> $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));<🎜> imagesetpixel($im,rand(0,$width),rand(0,$height),$color);<🎜> }<🎜> //Draw numbers<🎜> for($i=0;$i<$num;$i++) { <🎜> imagestring($im,5,mt_rand($i*$length+1,($i+1)*$length-8),mt_rand(0,$height-14),$_SESSION['code'][$i ],$white);<🎜> }<🎜> <🎜> //Output picture<🎜> header("content-type:image/jpeg");<🎜> imagejpeg($im);<🎜> }<🎜> <🎜> <🎜> <🎜> <🎜> - Main function: <🎜> imagecreatetruecolor //Create a new drawing board with a black background<🎜> imagecolorallocate //Get color<🎜> imagefill imagefill //Fill the artboard<🎜> imageline imageline //Draw a line<🎜> imagesetpixel //Draw points<🎜> imagestring imagestring //Draw a string (this verification code is a number)<🎜> Note: When drawing the digital verification code on the drawing board, please pay attention to the font out of bounds, so you need to subtract some range accordingly. See the attachment for details. <🎜> -Partial refresh verification code: <🎜>
The code is as follows<🎜> Copy code<🎜> <🎜>
        

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629689.htmlTechArticleGenerally when using the verification code, we have to implement partial refresh. This is inevitable and also improves the user experience. One point, let’s take a look at the usage of a PHP verification code in practical applications...
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