当前位置:首页 > PHP教程 > php应用 > 列表

php中文汉字验证码程序

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-23 14:38:06 浏览: 评论:0 

本文章利用三个文件来简单的讲了一下关于php中怎么去应用中文验证码,中文因为是汉字可能出现乱码所以我们就定义了一个文件来专门处理,有需要的朋友可以参考下.

php中文汉字验证码程序代码如下:

  1. <?php     
  2. /*     
  3. * 文件:chinesechar.php     
  4. * 作用:汉字数据储存     
  5. */     
  6. $ChineseChar = array("人","出","来","友","学","孝","仁","义","礼","廉","忠","国","中","易","白","者","火 ","土","金","木","雷","风","龙","虎","天","地",   "生","晕","菜","鸟","田","三","百","钱","福 ","爱","情","兽","虫","鱼","九","网","新","度","哎","唉","啊","哦","仪","老","少","日",   "月 ","星");     
  7. ?>    
  8.  
  9. <?php     
  10. /*     
  11. * 文件:check.php     
  12. * 作用:验证    
  13.  
  14. */     
  15. session_start();     
  16. $errorMSG = '';     
  17. //验证用户输入是否和验证码一致     
  18. if(!is_null($_POST['check']))     
  19. {     
  20.         if (strcasecmp($_SESSION['code'],$_POST['code'])==0)     
  21.             $errorMSG = "<p style="font-size:12px;color:#009900">验证成功!</p>";     
  22.         else     
  23.             $errorMSG = "<p style="font-size:12px;color:#FF0000">验证失败!</p>";     
  24. }     
  25. ?>     
  26. <html>     
  27. <head>     
  28. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">     
  29. </head>     
  30. <body>     
  31. <?php     
  32. if($errorMSG){     
  33. echo $errorMSG;     
  34. }     
  35. ?>     
  36. <form action=<?php echo $_SERVER['PHP_SELF']?> method=post>     
  37. 请输入验证码:<input type="text" name="code" style="width:     
  38. 80px"><img src="code.php">     
  39. <br>     
  40. <input type="submit" name="check" value="提交验证码">     
  41. </form>     
  42. </body>     
  43. </html>    
  44.  
  45. <?php     
  46. /*     
  47. * 文件:code.php     
  48. * 作用:验证码生成     
  49.    
  50. * 特注:由 没牙的草 指导 版权所有转载注明出处!有付出才会有收获!   
  51. */     
  52. include_once("chinesechar.php");     
  53. session_start();     
  54. // 设置 content-type     
  55. header("Content-type: image/png");     
  56. // 创建图片     
  57. $im = imagecreatetruecolor(120, 30);    
  58.  
  59. // 创建颜色     
  60. $fontcolor = imagecolorallocate($im, 255, 255, 255);     
  61. $bg = imagecolorallocate($im, 0, 0, 0);    
  62.  
  63. // 设置文字     
  64. for($i=0;$i<4;$i++) $text .= $ChineseChar[(array_rand($ChineseChar))];    
  65.  
  66. $_SESSION['code'] = $text;     
  67. // 设置字体     
  68. $font = 'simkai.ttf';    
  69. //开源代码phpfensi.com 
  70. // 添加文字     
  71. imagettftext($im, 18, 0, 11, 21, $fontcolor$font, iconv("GB2312","UTF-8",$text));    
  72.  
  73. // 输出图片     
  74. imagepng($im);     
  75. imagedestroy($im);     
  76. ?> 

如果想把上面的程序改成英文数字,只要在chinesechar.php 里面的数组中文改成数字或字母就KO了.

Tags: php中文汉字 php验证码

分享到: