搜索
首页后端开发php教程php图片验证码代码多个实例

  1. //文件头...

  2. header("Content-type: image/png");
  3. //创建真彩色白纸
  4. $im = @imagecreatetruecolor(50, 20) or die("建立图像失败");
  5. //获取背景颜色
  6. $background_color = imagecolorallocate($im, 255, 255, 255);
  7. //填充背景颜色(这个东西类似油桶)
  8. imagefill($im,0,0,$background_color);
  9. //获取边框颜色
  10. $border_color = imagecolorallocate($im,200,200,200);
  11. //画矩形,边框颜色200,200,200
  12. imagerectangle($im,0,0,49,19,$border_color);
  13. //逐行炫耀背景,全屏用1或0

  14. for($i=2;$i //获取随机淡色
  15. $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
  16. //画线
  17. imageline($im,2,$i,47,$i,$line_color);
  18. }
  19. //设置字体大小

  20. $font_size=12;
  21. //设置印上去的文字

  22. $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  23. $Str[1] = "abcdefghijklmnopqrstuvwxyz";
  24. $Str[2] = "01234567891234567890123456";
  25. //获取第1个随机文字

  26. $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
  27. $imstr[0]["x"] = rand(2,5);
  28. $imstr[0]["y"] = rand(1,4);
  29. //获取第2个随机文字

  30. $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];
  31. $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
  32. $imstr[1]["y"] = rand(1,3);
  33. //获取第3个随机文字

  34. $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
  35. $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);
  36. $imstr[2]["y"] = rand(1,4);
  37. //获取第4个随机文字

  38. $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
  39. $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);
  40. $imstr[3]["y"] = rand(1,3);
  41. //写入随机字串

  42. for($i=0;$i //获取随机较深颜色
  43. $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
  44. //画文字
  45. imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color);
  46. }
  47. //显示图片

  48. imagepng($im);
  49. //销毁图片
  50. imagedestroy($im);
  51. ?>
复制代码

例2,漂亮的PHP图片验证码实例 完整代码:

  1. /*
  2. * @Author fy
  3. */
  4. $imgwidth =100; //图片宽度
  5. $imgheight =40; //图片高度
  6. $codelen =4; //验证码长度
  7. $fontsize =20; //字体大小
  8. $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
  9. $font = 'Fonts/segoesc.ttf';
  10. $im=imagecreatetruecolor($imgwidth,$imgheight);
  11. $while=imageColorAllocate($im,255,255,255);
  12. imagefill($im,0,0,$while); //填充图像
  13. //取得字符串
  14. $authstr='';
  15. $_len = strlen($charset)-1;
  16. for ($i=0;$i $authstr .= $charset[mt_rand(0,$_len)];
  17. }
  18. session_start();
  19. $_SESSION['scode']=strtolower($authstr);//全部转为小写,主要是为了不区分大小写
  20. //随机画点,已经改为划星星了
  21. for ($i=0;$i $randcolor=imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  22. imagestring($im,mt_rand(1,5), mt_rand(0,$imgwidth),mt_rand(0,$imgheight), '*',$randcolor);
  23. //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
  24. }
  25. //随机画线,线条数量=字符数量(随便)
  26. for($i=0;$i{
  27. $randcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  28. imageline($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$randcolor);
  29. }
  30. $_x=intval($imgwidth/$codelen); //计算字符距离
  31. $_y=intval($imgheight*0.7); //字符显示在图片70%的位置
  32. for($i=0;$i $randcolor=imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
  33. //imagestring($im,5,$j,5,$imgstr[$i],$color3);
  34. // imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
  35. imagettftext($im,$fontsize,mt_rand(-30,30),$i*$_x+3,$_y,$randcolor,$font,$authstr[$i]);
  36. }
  37. //生成图像
  38. header("content-type:image/PNG");
  39. imagePNG($im);
  40. imageDestroy($im);
复制代码

例3,php5 图片验证码实例代码

php5生成图片验证码的例子。

需要用到php GD库函数: 1,imagecreatetruecolor -----创建一个真彩色的图像 imagecreatetruecolor(int x_size,int y_size) //x表示宽,y表示高 2,imagecolorallocate 为一幅图像分配颜色(调色板) imagecolorallocate(resource image,int red,int green,int blue)//red,green,blue----三原色 3,imagestring 绘图函数 iamgestring(resource image,font,int x,int y,内容,颜色); 4,输出函数 php的header是定义头的动作,php5中支持3中类型: 1,Content-type:xxxx/yyyy 2,Location:xxxx:yyyy/zzzz 3,Status:nnn xxxxxx xxxx/yyyy表示内容文件的类型 如:image/gif image/jpeg image/png 例子:header("Content-type:image/jpeg") GD库中有对应的image类型 imagejpeg(),imagegif(),imagepang() 5,imageline画线函数 iamgeline(resource image,int x1,int y1,int x2,int y2,int color); image ---图片 x1 ---启始坐标 y1 x2 ---终点坐标 y2 6,imagesetpixel画点函数 imagesetpixel(resource image,int x,int y,int color) 7,imagettftext带字体的写入函数 imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text) 8,php验证码插入中文的方法 iconv("gb2312","utf-8","字符串"); //首先要将文字转换成utf-8格式 9,随机函数 1,rand([int min,int max]) //rand(1,4) 生成1-4的数 2, dechex(十进制数) //转换为十六进制

生成验证码的步骤: 生成随机数 -- 创建图片 -- 随机数写成图片 --保存在session中

输入验证码例子 gdchek.php

  1. /*
  2. * 生成图片验证码
  3. * and open the template in the editor.
  4. */
  5. session_start();
  6. for($i=0;$i$rand.=dechex(rand(1,15)); //生成4位数包含十六进制的随机数
  7. }
  8. $_SESSION[check_gd]=$rand;
  9. $img=imagecreatetruecolor(100,30); //创建图片
  10. $bg=imagecolorallocate($img,0,0,0); //第一次生成的是背景颜色
  11. $fc=imagecolorallocate($img,255,255,255); //生成的字体颜色
  12. //给图片画线
  13. for($i=0;$i$te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  14. imageline($img,rand(0,15),0,100,30,$te);
  15. }
  16. //给图片画点
  17. for($i=0;$i$te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  18. imagesetpixel($img,rand()%100,rand()%30,$te);
  19. }
  20. //首先要将文字转换成utf-8格式
  21. //$str=iconv("gb2312","utf-8","呵呵呵");
  22. //加入中文的验证
  23. //smkai.ttf是一个字体文件,为了在别人的电脑中也能起到字体作用,把文件放到项目的根目录,可以下载,还有本机C:\WINDOWS\Fonts中有
  24. imagettftext($img,11,10,20,20,$fc,"simkai.ttf","你好你好");
  25. //把字符串写在图片中
  26. //imagestring($img,rand(1,6),rand(3,70),rand(3,16),$rand,$fc);
  27. //输出图片
  28. header("Content-type:image/jpeg");
  29. imagejpeg($img);
  30. ?>
复制代码

login.php

  1. /*
  2. *
  3. *
  4. */
  5. session_start();
  6. if($_POST[sub]){
  7. //判断验证码是否相同
  8. if($_POST[gd_pic]==$_SESSION[check_gd]){
  9. echo "验证成功!";
  10. }else{
  11. echo "验证码错误";
  12. }
  13. }
  14. ?>
  15. 用户名:
  16. 密码:
  17. 验证码:php图片验证码代码多个实例
复制代码


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
绝对会话超时有什么区别?绝对会话超时有什么区别?May 03, 2025 am 12:21 AM

绝对会话超时从会话创建时开始计时,闲置会话超时则从用户无操作时开始计时。绝对会话超时适用于需要严格控制会话生命周期的场景,如金融应用;闲置会话超时适合希望用户长时间保持会话活跃的应用,如社交媒体。

如果会话在服务器上不起作用,您将采取什么步骤?如果会话在服务器上不起作用,您将采取什么步骤?May 03, 2025 am 12:19 AM

服务器会话失效可以通过以下步骤解决:1.检查服务器配置,确保会话设置正确。2.验证客户端cookies,确认浏览器支持并正确发送。3.检查会话存储服务,如Redis,确保其正常运行。4.审查应用代码,确保会话逻辑正确。通过这些步骤,可以有效诊断和修复会话问题,提升用户体验。

session_start()函数的意义是什么?session_start()函数的意义是什么?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

为会话cookie设置httponly标志的重要性是什么?为会话cookie设置httponly标志的重要性是什么?May 03, 2025 am 12:10 AM

设置httponly标志对会话cookie至关重要,因为它能有效防止XSS攻击,保护用户会话信息。具体来说,1)httponly标志阻止JavaScript访问cookie,2)在PHP和Flask中可以通过setcookie和make_response设置该标志,3)尽管不能防范所有攻击,但应作为整体安全策略的一部分。

PHP会议在网络开发中解决了什么问题?PHP会议在网络开发中解决了什么问题?May 03, 2025 am 12:02 AM

phpsessions solvathepromblymaintainingStateAcrossMultipleHttpRequestsbyStoringDataTaNthEserVerAndAssociatingItwithaIniquesestionId.1)他们储存了AtoredAtaserver side,通常是Infilesordatabases,InseasessessionIdStoreDistordStoredStoredStoredStoredStoredStoredStoreDoreToreTeReTrestaa.2)

可以在PHP会话中存储哪些数据?可以在PHP会话中存储哪些数据?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,数字,数组和原始物。

您如何开始PHP会话?您如何开始PHP会话?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考虑使用AttActAcks.s.s.4)

什么是会话再生,如何提高安全性?什么是会话再生,如何提高安全性?May 02, 2025 am 12:15 AM

会话再生是指在用户进行敏感操作时生成新会话ID并使旧ID失效,以防会话固定攻击。实现步骤包括:1.检测敏感操作,2.生成新会话ID,3.销毁旧会话ID,4.更新用户端会话信息。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器