Home  >  Article  >  Backend Development  >  PHP generates a verification code program with interference_PHP tutorial

PHP generates a verification code program with interference_PHP tutorial

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

Here we introduce a relatively high-security verification generation program, which can include interference lines and other content, which can effectively prevent users from using the program to identify the verification code.

for($i=0;$i<10;$i++){ //Draw interference lines, 10 lines
imageline($im,rand(0,60),2,rand(0,60),20,$yellow);
The code is as follows
 代码如下 复制代码

/*
* Created on 2011-3-11
* Programmer : xiaoyao, QQ:1045195056
验证通过判断输入值与$_SESSION['check_pic']值
*/
session_start();
function RandAscii($number){//$number产生数字和字母个数
$arr=array('0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','w','v','u','x','y','z');
for ($i=1;$i<=$number;$i++)
{
$rand= $rand.$arr[rand(0,35)];
}
return $rand;
}
$rand=RandAscii(4);

$_SESSION['check_pic']=$rand;//随机产生的四个数赋值session中,用于验证。
$x=80;
$y=24;
$im=imagecreatetruecolor($x,$y);//创建图片
$bg=imagecolorallocate($im,255,255,255);//设置颜色背景
imagefill( $im,0,0,$bg);
$wh=imagecolorallocate($im,255,255,0);
$grey=imagecolorallocate($im,128,128,128);
$yellow=imagecolorallocate($im,255,255,0);
$red=imagecolorallocate($im,0,255,0);
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
);//字颜色数组
//画边框
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $x - 1, $y - 1, $border);

for($i=0;$i<10;$i++){ //画干扰线,10条
imageline($im,rand(0,60),2,rand(0,60),20,$yellow);

}
for($j=0;$j<100;$j++){
imagesetpixel($im,rand()%76,rand()%20,$red);
}
//imagestring($im,6,15,8,$rand,$wh);//字体大小1-5
imagettftext($im, 14,rand(30, -30), 5, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[0]);
imagettftext($im, 14,rand(50, -50), 24, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[1]);
imagettftext($im, 14,rand(50, -50), 43, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[2]);
imagettftext($im, 14,rand(30, -30), 62, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[3]);

header("Content-type: image/jpeg");//输出图片
imagejpeg($im);
imagedestroy($im);
?>

Copy code

 代码如下 复制代码
/*
* Created on 2011-3-11
* Programmer : xiaoyao, QQ:1045195056
验证通过判断输入值与$_SESSION['check_pic']值
*/
session_start();//开启session
if(isset($_POST['check']))
{
if($_POST['check'])
{
if($_POST['check']==$_SESSION['check_pic'])
{
echo " 验证码正确".$_SESSION['check_pic'];
}
else
{
echo " 验证码错误".$_SESSION['check_pic'];
}
}
}
?>


   


/*
* Created on 2011-3-11
* Programmer: xiaoyao, QQ:1045195056
Verify by judging the input value and $_SESSION['check_pic'] value
*/
session_start();
function RandAscii($number){//$number generates the number of numbers and letters
$arr=array('0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m ','n','o','p','q','r','s','t','w','v','u','x','y', 'z');
for ($i=1;$i<=$number;$i++)
{
$rand= $rand.$arr[rand(0,35)];
}
return $rand;
}
$rand=RandAscii(4);

$_SESSION['check_pic']=$rand;//Four randomly generated numbers are assigned to the session for verification.
$x=80;
$y=24;
$im=imagecreatetruecolor($x,$y);//Create image
$bg=imagecolorallocate($im,255,255,255);//Set color background
imagefill( $im,0,0,$bg);
$wh=imagecolorallocate($im,255,255,0);
$grey=imagecolorallocate($im,128,128,128);
$yellow=imagecolorallocate($im,255,255,0);
$red=imagecolorallocate($im,0,255,0);
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
);//Word color array
//Draw a border
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $x - 1, $y - 1, $border);
<🎜>}
for($j=0;$j<100;$j++){
imagesetpixel($im,rand()%76,rand()%20,$red);
}
//imagestring($im,6,15,8,$rand,$wh);//Font size 1-5
imagettftext($im, 14,rand(30, -30), 5, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[0]);
imagettftext($im, 14,rand(50, -50), 24, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[1]);
imagettftext($im, 14,rand(50, -50), 43, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[2]);
imagettftext($im, 14,rand(30, -30), 62, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[3]); <🎜> <🎜>header("Content-type: image/jpeg");//Output image
imagejpeg($im);
imagedestroy($im);
?> Call method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629697.htmlTechArticleHere we introduce a relatively secure verification generation program, which can bring interference lines and other content, and can be effective Prevent users from using programs to identify verification codes. The 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