PHP中怎么实现中文汉字验证码

发布时间:2021-06-24 18:02:45 作者:Leah
来源:亿速云 阅读:217

PHP中怎么实现中文汉字验证码,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

  1.算式验证码:

  session_start();

  header("Content-type:image/png");

  $num1=mt_rand(0,9);//第一位数

  $num2=mt_rand(1,9);//第二位数

  $type_str="+-*";//方法字符串集合

  $type=substr($type_str,rand(0,2),1);//随机方法

  $change=mt_rand(1,3);

  if($change==1){

  $code="$num1$type$num2=";

  $result="\$verifyCode=$num1$type$num2;";

  eval($result);

  $_SESSION['authnum_session']=$verifyCode;

  }elseif($change==2){

  $result="\$verifyCode=$num1$type$num2;";

  eval($result);

  $code=$num1.$type."_=".$verifyCode;

  $_SESSION['authnum_session']=$num2;

  }elseif($change==3){

  $result="\$verifyCode=$num1$type$num2;";

  eval($result);

  $code="_".$type.$num2."=".$verifyCode;

  $_SESSION['authnum_session']=$num1;

  }

  $im=imagecreate(68,28);

  $black=imagecolorallocate($im,0,0,0);

  $white=imagecolorallocate($im,255,255,255);

  $gray=imagecolorallocate($im,200,200,200);

  $red=imagecolorallocate($im,255,0,0);

  imagefill($im,0,0,$white);

  imagestring($im,5,10,8,$code,$black);

  for($i=0;$i<70;$i++){>

  PHP中文汉字验证码怎么实现

  session_start();

  $ch_str="的一是在了不和有大这主中人上为们地个用工时要动国产以我到他会作来分生对于学下级就年阶义发成部民

  $len=mb_strlen($ch_str,"utf-8");//汉字长度

  $str=array();

  for($i=0;$i<4;$i++){   $pos=mt_rand(0,$len-5);//开始位置   $str[]=mb_substr($ch_str,$pos,1,"utf-8");   }   $authnum_session=implode("",$str);   $_SESSION['authnum_session']=$authnum_session;//记录到session   Header("Content-type:image/PNG");   //图片的长和高   $image_x=100;   $image_y=50;   $im=imagecreate($image_x,$image_y);   //这里取图片底色为白色   $bkg=ImageColorAllocate($im,255,255,255);   //显示的字体样式,这个要把文件放到对应的目录中,假如你没有文件就去window的字体文件中找一个吧。   $fnt="simhei.ttf";   //为图像分配一些颜色   $white=ImageColorAllocate($im,234,185,95);   //在图片上画椭圆弧,指定下坐标点   imagearc($im,150,8,20,20,75,170,$white);   imagearc($im,180,7,50,30,75,175,$white);   //在图片上画一条线段,指定下坐标点   imageline($im,20,20,180,30,$white);   imageline($im,20,18,170,50,$white);   imageline($im,25,50,80,50,$white);   //乱点的数量   $noise_num=3000;   $line_num=50;   //各种混乱字符的颜色   $rectangle_color=imagecolorallocate($im,0xAA,0xAA,0xAA);   $noise_color=imagecolorallocate($im,0x00,0x00,0x00);   $font_color=imagecolorallocate($im,0x00,0x00,0x00);   for($i=0;$i<$noise_num;$i++)   {   //在一个坐标点上画一个单一像素,这个点上面定义了,是黑色的。   //imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);   }   for($i=0;$i<$line_num;$i++)   {   $line_color=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));   //在两个坐标点间画一条线,颜色在上面定义   imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);   }   for($i=0;$i<4;$i++)>

  注意:

  中文汉字验证码单独执行,然后获取session会发现验证码和session内容不一致。但是在img标签中src属性中引用这个中文汉字验证码文件时,然后获取session,这时两者内容时一致的。

关于PHP中怎么实现中文汉字验证码问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. php统计中文汉字长度
  2. php 正则对于中文汉字字符的提取

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:Java中怎么调用SqlServer存储过程

下一篇:bootstrap中怎么实现响应式布局

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》