分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

PHP实现验证码制作

发布时间:2023-09-06 02:04责任编辑:胡小海关键词:PHP验证码

captcha.php(PHP产生验证码并储存Session):

 1 <?php 2 ??3 ??4 ??5 //开启Session 6 session_start(); 7 ??8 ??9 ?10 //绘制底图11 $image = imagecreatetruecolor(100, 30);//返回资源型的值12 $bgcolor = imagecolorallocate($image, 255, 255, 255);//创建一个底图13 imagefill($image, 0, 0, $bgcolor);//区域填充14 ?15 ?16 ?17 /*18 //输出随机数字19 for($i = 0; $i < 4; $i++){20 ????$fontsize = 6;//字体大小21 ????$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//字体颜色22 ????$fontcontent = rand(0, 9);//字符串内容23 ?????24 ????$x = ($i*100/4) + rand(5, 10);//数字的横坐标25 ????$y = rand(5, 10);//数字的纵坐标26 ?????27 ????imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);//在图像资源上绘制字符28 }29 */30 ?31 ?32 ?33 //产生随机字符串34 $captch_code = ‘‘;35 for($i = 0; $i < 4; $i++){36 ????$fontsize = 6;//字体大小37 ????$fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120),rand(0, 120));//字体颜色38 ?39 ????$data = ‘23456789ABCDEFGHJKLMNOPQRTUVWXYZabcdefghjkmnopqrtuvwxy‘;//随机字符串的字典40 ????$fontcontent = substr($data, rand(0, strlen($data)), 1);//字符41 ????$captch_code .= $fontcontent;//拼接字符串42 ?????43 ????$x = ($i*100/4) + rand(5, 10);//字符横坐标44 ????$y = rand(5, 10);//字符纵坐标45 ?????46 ????//在图像资源上绘制字符47 ????imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);48 }49 //将验证码字符串存储在Session50 $_SESSION[‘authcode‘] = $captch_code;51 ?52 ?53 ?54 ?55 //点干扰56 for($i = 0; $i < 200; $i++){57 ????$pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));58 ????imagesetpixel($image, rand(1, 99), rand(1, 99), $pointcolor);59 }60 ?61 ?62 ?63 //线干扰64 for($i = 0; $i < 6; $i++){65 ????$linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));66 ????imageline($image, rand(1, 99), rand(1, 99), rand(1, 99), rand(1, 99), $linecolor);67 }68 ?69 ?70 ?71 //输出图片内容72 header(‘content-type:image/png‘);//输出内容的格式73 imagepng($image);//输出内容74 imagedestroy($image);//销毁资源75 ?76 ?77 ?78 ?79 ?80 ?>

captcha-form.html(Web表单验证):

 1 <!DOCTYPE html> 2 <html> 3 <head> 4 ??<meta charset="utf-8"> 5 ??<title>验证码</title> 6 </head> 7 ??8 <body> 9 ??<form method="post" action="./captcha-result.php">10 ?????11 ????<p>12 ??????验证码图片:13 ??????<img id="captcha-img" border="1" src="./captcha.php?r=0">14 ??????<a href="javascript:void(0);" onclick="getCaptchaImg();">看不清</a>15 ????</p>16 ?????17 ????<p>请输入图片中的内容:<input id="authcode" type="text" name="authcode" value=""></p>18 ?????19 ????<p><input type="submit" value="提交" style="padding:6px 20px;"></p>20 ???21 ??</form>22 ?23 ??<script>24 ????//动态获取验证码25 ????function getCaptchaImg(){26 ????????//从服务器获取新的验证码27 ????????document.getElementById(‘captcha-img‘).src=‘./captcha.php?r=‘+Math.random();28 ????????//清空文本框里已输入的内容29 ????????document.getElementById(‘authcode‘).value="";30 ????}31 ??</script>32 ?33 </body>34 </html>

captcha-result.php(PHP判断验证码是否正确):

 1 <?php 2 ??3 ??4 ??5 //验证验证码是否正确 6 if(isset($_REQUEST[‘authcode‘])){ 7 ????//开启Session 8 ????session_start(); 9 ?????10 ????//strtolower()将字符串都转换成小写,将验证码设置为不区分大小写型11 ????if(strtolower($_REQUEST[‘authcode‘]) == strtolower($_SESSION[‘authcode‘])){12 ????????echo "输入正确";13 ????}else{14 ????????echo "输入错误";15 ????}16 ????exit();17 }18 ?19 ?20 ?21 ?>

所用到的函数原型:

 1 <?php 2 ??3 ??4 ??5 //imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。 6 resource imagecreatetruecolor ( int $width , int $height ); 7 ??8 ??9 ?10 //imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。11 int imagecolorallocate ( resource $image , int $red , int $green , int $blue );12 ?13 ?14 ?15 //imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。16 bool imagefill ( resource $image , int $x , int $y , int $color );17 ?18 ?19 ?20 //imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。21 bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col );22 ?23 ?24 ?25 //imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。26 bool imagesetpixel ( resource $image , int $x , int $y , int $color );27 ?28 ?29 ?30 //imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。31 bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color );32 ?33 ?34 ?35 //imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。36 bool imagepng ( resource $image [, string $filename ] );37 ?38 ?39 ?40 //imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor()。41 bool imagedestroy ( resource $image );42 ?43 ?44 ?45 ?>

PHP实现验证码制作

原文地址:https://www.cnblogs.com/tong707/p/php-captcha-image.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved