当前位置:Gxlcms > PHP教程 > php实现的简单中文验证码功能示例

php实现的简单中文验证码功能示例

时间:2021-07-01 10:21:17 帮助过:32人阅读

这篇文章主要介绍了php实现的简单中文验证码功能,结合实例形式分析了php中文验证码功能的实现步骤与操作方法,包括图形创建、编码操作、session操作等相关技巧,需要的朋友可以参考下

本文实例讲述了php实现的简单中文验证码功能。分享给大家供大家参考,具体如下:

img.php

  1. <?php
  2. session_start();
  3. /*for($i=0;$i<4;$i++) {
  4. $rand .= dechex(rand(1,15));
  5. }
  6. $_SESSION[check_pic] = $rand;
  7. */
  8. $image = imagecreatetruecolor(100, 30);
  9. $bg = imagecolorallocate($image, 0, 0, 0);
  10. $color = imagecolorallocate($image, 255, 255, 255);
  11. //imagestring($image, rand(1,6), rand(3,60), rand(3,15), $rand, $color);
  12. for($i=0;$i<3;$i++) {
  13. $color2 = imagecolorallocate($image, rand(0,255), rand(0,255),rand(0,255));
  14. imageline($image, rand(0,100), 0, 100, 30, $color2);
  15. }
  16. //rand() ---->0-max 不大于100
  17. for($i=0;$i<200;$i++) {
  18. imagesetpixel($image, rand()%100, rand()%30, $color2);
  19. }
  20. //$str = iconv("gbk", "utf-8", "中");
  21. $str = "中国";
  22. $_SESSION[check_pic] = $str;
  23. //解决中文,页面本身为utf-8
  24. $str = mb_convert_encoding($str, "html-entities","utf-8" );
  25. //2:字体大小 3:倾斜角度 x , y 坐标
  26. imagettftext($image, 12, 0, 20, 20, $color, 'MSYH.TTF', $str);
  27. //
输出图片 header("Content-type: image/jpeg;charset=utf-8"); imagejpeg($image); /*修改eclipse的配置,可以使得eclipse的新建项目的默认编码直接为UTF-8 在菜单栏的 Window->Preferences->General->Workspace->Text file encoding 将其改为UFT-8即可。*/ ?>

sub.php

  1. <?php
  2. header("Content-type: text/html;charset=utf-8");
  3. session_start();
  4. if($_POST[check]) {
  5. if($_POST[check]==$_SESSION[check_pic]) {
  6. echo "验证码正确:".$_SESSION[check_pic];
  7. } else {
  8. echo "验证码错误:".$_SESSION[check_pic];
  9. }
  10. }
  11. ?>
  12. <form action="" method="post">
  13. <img alt="" src="img.php"><br/>
  14. <input type="text" name="check"><br/>
  15. <input type="submit" value="提交">
  16. </form>

运行效果图如下:

相关推荐:

PHP实现购物网站


以上就是php实现的简单中文验证码功能示例的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行