Home  >  Article  >  Backend Development  >  PHP entry-level PHP verification code program_PHP tutorial

PHP entry-level PHP verification code program_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:50:52866browse

We will use PHP graphics processing functions to generate verification codes, such as imagecreate, imagepng, header and other functions. Let's take a look at a simple example.

Examples

The code is as follows Copy code
 代码如下 复制代码

session_start();

$im = imagecreate(80,30);//创建图片
$color = imagecolorallocate($im,rand(150,200),rand(150,200),rand(150,200));//设置图片背景
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";//产生随机字符串
for($i=0;$i<5;$i++){
$code .= $str[rand(0,(strlen($str)-1))];
}
$_SESSION['code'] = $code;
for($a=0;$a<5;$a++){ //将字符串写入图片资源
$x = $a*10 + 15;
$y = rand(5,10); // www.bKjia.c0m
imagechar($im,5,$x,$y,$code{$a},imagecolorallocate($im,0,0,0));
}
header("Content-type:image/png");//输出图片资源
imagepng($im);
?>

session_start();

$im = imagecreate(80,30);//Create image
$color = imagecolorallocate($im,rand(150,200),rand(150,200),rand(150,200));//Set the picture background
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";//Generate a random string
for($i=0;$i<5;$i++){
$code .= $str[rand(0,(strlen($str)-1))];
                                                     $_SESSION['code'] = $code;
for($a=0;$a<5;$a++){ //Write string into image resource
$x = $a*10 + 15;
$y = rand(5,10); // www.bKjia.c0m
Imagechar($im,5,$x,$y,$code{$a},imagecolorallocate($im,0,0,0));
}  
header("Content-type:image/png");//Output image resources
imagepng($im);
?>

Example 2
 代码如下 复制代码

if(!isset($_SESSION)){ //判断session是否开启
session_start(); //开启就session
}
$width=70; //布画宽度
$height=25; //布画高度
$length=4;//验证码长度
$code=getcode($length); //获取随机字符串
$_SESSION['verfyCode'] = $code;

$img=imagecreate($width,$height);
$bgcolor=imagecolorallocate($img,240,240,240);
$rectangelcolor=imagecolorallocate($img,150,150,150);
imagerectangle($img,1,1,$width-1,$height-1,$rectangelcolor);//画边框
for($i=0;$i<$length;$i++){//循环写字
$codecolor=imagecolorallocate($img,mt_rand(50,200),mt_rand(50,128),mt_rand(50,200));
$angle=rand(-20,20);
$charx=$i*15+8;
$chary=($height+14)/2+rand(-1,1);
imagettftext($img,15,$angle,$charx,$chary,$codecolor,'C:WINDOWSFontsSIMKAI.TTF',
$code[$i]);
}
for($i=0;$i<20;$i++){//循环画线
$linecolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
$linex=mt_rand(1,$width-1);
$liney=mt_rand(1,$height-1);
imageline($img,$linex,$liney,$linex+mt_rand(0,4)-2,$liney+mt_rand(0,4)-2,$linecolor);
}
for($i=0;$i<100;$i++){//循环画点
$pointcolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
imagesetpixel($img,mt_rand(1,$width-1),mt_rand(1,$height-1),$pointcolor);
}
function getcode($length){//生成php随机数
$pattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';//字符池
for($i=0;$i<$length;$i++) {
$key .= $pattern{mt_rand(0,35)};
}
return $key;

}
ob_clean();
header('Content-type:image/png');
imagepng($img);
?>

The code is as follows Copy code
if(!isset($_SESSION)){ //Determine whether the session is open
session_start(); //Open session
}
$width=70; //Width of canvas
$height=25; //Height of canvas
$length=4;//Verification code length
$code=getcode($length); //Get a random string
$_SESSION['verfyCode'] = $code;<🎜> <🎜>$img=imagecreate($width,$height);
$bgcolor=imagecolorallocate($img,240,240,240);
$rectangelcolor=imagecolorallocate($img,150,150,150);
imagerectangle($img,1,1,$width-1,$height-1,$rectangelcolor);//Draw a border
for($i=0;$i<$length;$i++){//Loop writing
$codecolor=imagecolorallocate($img,mt_rand(50,200),mt_rand(50,128),mt_rand(50,200));
$angle=rand(-20,20);
$charx=$i*15+8;
$chary=($height+14)/2+rand(-1,1);
imagettftext($img,15,$angle,$charx,$chary,$codecolor,'C:WINDOWSFontsSIMKAI.TTF',
$code[$i]);
}
for($i=0;$i<20;$i++){//Loop to draw lines
$linecolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
$linex=mt_rand(1,$width-1);
$liney=mt_rand(1,$height-1);
imageline($img,$linex,$liney,$linex+mt_rand(0,4)-2,$liney+mt_rand(0,4)-2,$linecolor);
}
for($i=0;$i<100;$i++){//Loop to draw points
$pointcolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
imagesetpixel($img,mt_rand(1,$width-1),mt_rand(1,$height-1),$pointcolor);
}
function getcode($length){//Generate php random numbers
$pattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';//Character pool
for($i=0;$i<$length;$i++) {
$key .= $pattern{mt_rand(0,35)};
}
return $key;<🎜> <🎜>}
ob_clean();
header('Content-type:image/png');
imagepng($img);
?>

The rendering is as follows

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632615.htmlTechArticleTo generate the verification code, we will use PHP graphics processing functions, such as imagecreate, imagepng, header and other functions. Let's look at a simple example together. Example code is as follows Copy the code...
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