Discuz教程网

精典的php验证码源代码

[复制链接]
authicon 不死不活 发表于 2010-12-3 17:17:07 | 显示全部楼层 |阅读模式
  1. <?php
  2. //Session保存路径
  3. $sessSavePath = dirname(__FILE__)."/../data/sessions/";
  4. if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ session_save_path($sessSavePath);}
  5. $letter = "";
  6. //获取随机数字
  7. for ($i=0; $i<3; $i++) {
  8.   $letter .= chr(mt_rand(48,57));
  9. }
  10. //获取随机字母
  11. for ($i=0; $i<3; $i++) {
  12.   $letter .= chr(mt_rand(65,90));
  13. }
  14. //重构字符顺序
  15. $strs = str_split($letter);
  16. shuffle ($strs);
  17. $rndstring = "";
  18. while (list (, $str) = each ($strs)) {
  19.   $rndstring .= $str;
  20. }
  21. //获取随机字符
  22. //$letras = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  23. //for($i=0; $i<6; $i++) {
  24.         //$rndstring .= chr(mt_rand(48,90));
  25. //        $rndstring .= $letras[rand(0, 35)];
  26. //}

  27. //如果支持GD,则绘图
  28. if(function_exists("imagecreate"))
  29. {
  30.         //PutCookie("dd_ckstr",strtolower($rndstring),1800,"/");
  31.         session_register('dd_ckstr');
  32.         $_SESSION['dd_ckstr'] = strtolower($rndstring);
  33.         $rndcodelen = strlen($rndstring);
  34.   //图片大小
  35.   //$im = imagecreate(85,20);
  36.   $im = imagecreatefromgif("code.gif");
  37.   //字体
  38.   $font_type = dirname(__FILE__)."/data/ant1.ttf";
  39.   //背景颜色
  40.   //$backcolor = imagecolorallocate($im,190,190,120);
  41.   //边框色
  42.   //$iborder = ImageColorAllocate($im, 0x71,0x76,0x67);

  43.   //字体色
  44.   //不支持 imagettftext
  45.   $fontColor = ImageColorAllocate($im, 0xff,0xff,0xff);
  46.   
  47.   //支持 imagettftext
  48.   $fontColor2 = ImageColorAllocate($im, 0xff,0xff,0xff);
  49.   //阴影
  50.   $fontColor1 = ImageColorAllocate($im, 0x33,0x33,0x33);
  51.   
  52.   //杂点背景线
  53.   //$lineColor1 = ImageColorAllocate($im, 130,220,245);
  54.   //$lineColor2 = ImageColorAllocate($im, 225,245,255);
  55.   
  56.   //背景线
  57.   //for($j=3; $j<=16; $j=$j+3) imageline($im,2,$j,83,$j,$lineColor1);
  58.   //for($j=2; $j<83; $j=$j+(mt_rand(3, 10))) imageline($im,$j,2,$j-6,18,$lineColor2);

  59.   //边框
  60.   //imagerectangle($im, 0, 0, 84, 19, $iborder);
  61.   
  62.   $strposs = array();
  63.   //文字
  64.   for($i=0;$i<$rndcodelen;$i++){
  65.           if(function_exists("imagettftext")){
  66.                   $strposs[$i][0] = $i*13+6;
  67.                   $strposs[$i][1] = mt_rand(15,18);
  68.                   imagettftext($im, 5, 5, $strposs[$i][0]+1, $strposs[$i][1]+1, $fontColor1, $font_type, $rndstring[$i]);
  69.           } else{
  70.                   imagestring($im, 5, $i*13+6, mt_rand(2, 4), $rndstring[$i], $fontColor);
  71.           }
  72.   }
  73.   
  74.   
  75.   //文字
  76.   for($i=0;$i<$rndcodelen;$i++){
  77.           if(function_exists("imagettftext")){
  78.                   imagettftext($im, 11,5, $strposs[$i][0]-1, $strposs[$i][1]-1, $fontColor2, $font_type, $rndstring[$i]);
  79.           }
  80.   }
  81.   
  82.   
  83.   header("Pragma:no-cache\r\n");
  84.   header("Cache-Control:no-cache\r\n");
  85.   header("Expires:0\r\n");
  86.   //输出特定类型的图片格式,优先级为 gif -> jpg ->png
  87.   if(function_exists("imagejpeg")){
  88.           header("content-type:image/jpeg\r\n");
  89.           imagejpeg($im);
  90.   }else{
  91.     header("content-type:image/png\r\n");
  92.           imagepng($im);
  93.   }
  94.   ImageDestroy($im);

  95. }else{ //不支持GD,只输出字母 ABCD       
  96.         //PutCookie("dd_ckstr","abcd",1800,"/");
  97.         session_register('dd_ckstr');
  98.         $_SESSION['dd_ckstr'] = "abcd";
  99.         header("content-type:image/jpeg\r\n");
  100.         header("Pragma:no-cache\r\n");
  101.   header("Cache-Control:no-cache\r\n");
  102.   header("Expires:0\r\n");
  103.         $fp = fopen("./vdcode.jpg","r");
  104.         echo fread($fp,filesize("./vdcode.jpg"));
  105.         fclose($fp);
  106. }
  107. ?>
复制代码





上一篇:《PHP新手上路中文教程》与你分享
下一篇:PHP超强大的发邮件类
authicon kikiya11 发表于 2011-6-16 12:59:37 | 显示全部楼层
看看  好像不错
authicon kikiya11 发表于 2011-6-17 20:59:52 | 显示全部楼层
喜欢喜欢喜欢
authicon 乐娃娃 发表于 2011-6-18 14:00:01 | 显示全部楼层
顶你一下,好贴要顶!
authicon huanglv80 发表于 2011-6-18 20:00:08 | 显示全部楼层
很好 很强大。。谢谢分享
authicon haidideyu 发表于 2011-6-21 14:59:53 | 显示全部楼层
支持一下,确实是不错的贴子。
authicon fantuanzi 发表于 2011-6-25 18:00:02 | 显示全部楼层
不错,谢谢分享
authicon 婷婷爱牛牛 发表于 2011-6-26 15:59:46 | 显示全部楼层
好东西,要下来看看
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 07:28

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表