Home  >  Article  >  Backend Development  >  How to use PHP drawing function to implement simple verification code function_php example

How to use PHP drawing function to implement simple verification code function_php example

WBOY
WBOYOriginal
2016-12-05 13:28:211124browse

index.php

<&#63;php
//===================================》》使用绘图技术绘制验证码

//1.随机产生4个随机数
$checkCode="";
for ($i=0;$i<4;$i++){
$checkCode.=dechex(rand(1, 15));// decheck()十进制转换为十六进制,即验证码上要显示的数字
}

//2.存入列
session_start();
$_SESSION['checkCode']=$checkCode;

//3.创建画布
$image1=imagecreatetruecolor(100, 30);

//制造干扰,创建20条弧线
for ($j=0;$j<30;$j++){
imagearc($image1, rand(0, 100), rand(0, 30), rand(0, 100), rand(0, 30), rand(0, 360), rand(0, 360), imagecolorallocate($image1, rand(0, 155), rand(0, 255), rand(0, 255)));
}

//3.创建字体颜色,将字粘贴上去
$white=imagecolorallocate($image1, 255, 255, 255);
imagestring($image1, rand(2, 5), rand(5, 70), rand(2, 15), $checkCode, $white);

//5.输出图像或保存
header("content-type:image/png");
imagepng($image1);

//6.释放资源
imagedestroy($image1);

login.php

请输入验证码:<img src="index.php" onclick="this.src='index.php&#63;a=+random()'">

The above is the entire method brought to you by the editor to use PHP drawing functions to implement simple verification code functions. I hope you will support Script Home~

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