Home  >  Article  >  Backend Development  >  Complete example of generating verification code and verification code in PHP_PHP tutorial

Complete example of generating verification code and verification code in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:40846browse

This article shares an example of a verification code that I use. From generating image verification codes to using verification code examples, students who need to learn can refer to this article.

hyml page

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





无标题文档



 
 
  验证码
  看不清?换一个
 





Untitled Document





Verification Code
Can’t see clearly? Change



 代码如下 复制代码
/*
图片验证码 Powered By KASON
*/
session_start();
$num=4;//验证码个数
$width=80;//验证码宽度
$height=20;//验证码高度
$code=' ';
for($i=0;$i<$num;$i++)//生成验证码
{
switch(rand(0,2))
{
case 0:$code[$i]=chr(rand(48,57));break;//数字
case 1:$code[$i]=chr(rand(65,90));break;//大写字母
case 2:$code[$i]=chr(rand(97,122));break;//小写字母
}
}
$_SESSION["VerifyCode"]=$code;
$image=imagecreate($width,$height);
imagecolorallocate($image,255,255,255);
for($i=0;$i<80;$i++)//生成干扰像素
{
$dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color);
}
for($i=0;$i<$num;$i++)//打印字符到图像
{
$char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
imagechar($image,60,($width/$num)*$i,rand(0,5),$code[$i],$char_color);
}
header("Content-type:image/png");
imagepng($image);//输出图像到浏览器
imagedestroy($image);//释放资源
?>   
The verifycode.php file code is as follows


The checkcode.php file is as follows

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


ini_set('display_errors', 'Off');
session_start();
if((strtoupper($_POST["code"])) == strtoupper(($_SESSION["VerifyCode"]))){
print("验证码正确,");
}else{
print("验证码错误,");
}
echo "提交的验证码:".strtoupper($_POST["code"]).",正确的验证码:".strtoupper($_SESSION["VerifyCode"]);
?>

Copy code
ini_set('display_errors', 'Off');
session_start(); print("Verification code is correct,"); }else{ Print("Verification code error,"); } echo "Submitted verification code:".strtoupper($_POST["code"]).", correct verification code: ".strtoupper($_SESSION["VerifyCode"]); ?>
http://www.bkjia.com/PHPjc/631291.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631291.htmlTechArticleThis article shares an example of a verification code that I use, from generating image verification codes to using verification codes. Students who need to learn can refer to this article. hyml page...
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