三款php 中文验证码生成与调用方法 在php中要生成中文验证码就必须做与生成验证验证码不一样的操作,因为GD函数只接受UTF8格式编码的文字,所以在用php生成中文验证码时面要前首先要进行编码转换,操作php的iconv可以实例。
三款php教程 中文验证码生成与调用方法
在php中要生成中文验证码就必须做与生成验证验证码不一样的操作,因为gd函数只接受utf8格式编码的文字,所以在用php生成中文验证码时面要前首先要进行编码转换,操作php的iconv可以实例。
*/
$ch_str="你要生成中文验证码汉字";
$str=array();
for ($i=0;$i{
$str[]=$ch_str[$i].$ch_str[$i+1].$ch_str[$i+2];
}
//图片的长和高
$image_x=200;
$image_y=100;
$im = imagecreate($image_x,$image_y);
//这里取图片底色为白色
$bkg = imagecolorallocate($im,255,255,255);
//显示的字体样式,这个要把文件放到对应的目录中,如果你没有文件就去window的字体文件中找一个吧。
$fnt = "simfang.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=80;
//各种混乱字符的颜色
$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 {
//在一个坐标点上画一个单一像素,这个点上面定义了,是黑色的。
imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
}for($i=0;$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);
}
$randnum=rand(0,count($str)-4);
//保持是偶数
if ($randnum%2)
{
$randnum+=1;
}
$str1=$str[$randnum].$str[$randnum+1];
for ($i=0;$i {
imagettftext($im, rand(28,32), rand(0,70), rand(($image_x/4)*$i+$image_x/10,($image_x/4)*$i+$image_x/8), rand($image_y/2+$image_y/10,$image_y/2+$image_y/5), $font_color, $fnt, $str[$randnum+$i]);
}
imagepng($im);
imagedestroy($im);//生成中文验证码二
$str="中文汉字";
$image_x=110;
$image_y=110;
$im = imagecreate($image_x,$image_y);
$bkg = imagecolorallocate($im,255,255,255);
$fnt = "hb.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=80;
imagecolorallocate($im,0xff,0xff,0xff);
$rectangle_color=imagecolorallocate($im,0xaa,0xaa,0xaa);
$noise_color=imagecolorallocate($im,0x00,0x00,0x00);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
$line_color=imagecolorallocate($im,0x00,0x00,0x00);
for($i=0;$i imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
for($i=0;$i imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
$randnum=rand(0,strlen($str)-4);
if ($randnum%2)$randnum+=1;
$str1=substr($str,$randnum,4);
$str2 = iconv("gb2312","utf-8",$str1);//验证汉字在$str1里面imagettftext($im, rand(28,32), rand(0,70), rand(25,27), rand(70,100), $font_color, $fnt, $str2);
imagepng($im);
imagedestroy($im);//把汉字放在数组
/*
gd函数只接受utf8格式编码的文字,所以在写文字前首先要进行编码转换。php自带的iconv和mbstring库都可以完成这项工作
*/$randcode=array('宠');
$codetable=array();
$fp=fopen("gb2312.txt","r");
while($line=fgets($fp))
$codetable[hexdec(substr($line,0,6))]=substr($line,7,6);
fclose($fp);//gb2312转utf8
function gb2utf8($gbstr)
{
global $codetable;
if(trim($gbstr)=="")
return $gbstr;
$ret="";
$utf8="";
while($gbstr)
{
if(ord(substr($gbstr,0,1))>127)
{
$thisw=substr($gbstr,0,2);
$gbstr=substr($gbstr,2,strlen($gbstr));
$utf8="";
@$utf8=u2utf8(hexdec($codetable[hexdec(bin2hex($thisw))-0x8080]));
if($utf8!="")
for($i=0;$i$ret.=chr(substr($utf8,$i,3));
}
else
{
$ret.=substr($gbstr,0,1);
$gbstr=substr($gbstr,1,strlen($gbstr));
}
}
return $ret;
}
//unicode转utf8
function u2utf8($c)
{
$str="";
if($c $str.=$c;
elseif($c {
$str.=(0xc0|$c>>6);
$str.=(0x80|$c&0x3f);
}
elseif($c {
$str.=(0xe0|$c>>12);
$str.=(0x80|$c>>6&0x3f);
$str.=(0x80|$c&0x3f);
}
elseif($c {
$str.=(0xf0|$c>>18);
$str.=(0x80|$c>>12&0x3f);
$str.=(0x80|$c>>6&0x3f);
$str.=(0x80|$c&0x3f);
}
return $str;
}//生成附加码
function create_excode($length)
{
global $randcode;
header("content-type: image/png");
$image_x=$length*30; //图片宽度
$image_y=40; //图片高度
$noise_num=80*$length; //杂点数量
$line_num=$length-2; //干扰线数量
$image=imagecreate($image_x,$image_y);
imagecolorallocate($image,0xff,0xff,0xff); //设定背景颜色
$rectangle_color=imagecolorallocate($image,0xaa,0xaa,0xaa); //边框颜色
$noise_color=imagecolorallocate($image,0x00,0x00,0x00); //杂点颜色
$font_color=imagecolorallocate($image,0x00,0x00,0x00); //字体颜色
$line_color=imagecolorallocate($image,0x33,0x33,0x33); //干扰线颜色//加入杂点
for($i=0;$i imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);$font_face="simkai.ttf"; //字体
$x=2;
$session_code='';
for($i=0;$i {
$code=$randcode[mt_rand(0,count($randcode)-1)];
imagettftext($image,18,mt_rand(-6,6),$x,29,$font_color,$font_face,gb2utf8($code));
$x+=30;
$session_code.=$code;
}
@session_start();
$_session['excode']=$session_code; //把附加码的值放在session中
//加入干扰线
for($i=0;$i imageline($image,mt_rand(0,$image_x),mt_rand(0,$image_y),
mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color); //加个边框
imagepng($image);
imagedestroy($image);
}
create_excode(6);
// 使用的时候直接用html语法:调用就可以了,在服务端做验证时取session存储的验证字符与用户提交的字符进行比较,相同则通过验证

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

Fibers在PHP8.1中引入,提升了并发处理能力。1)Fibers是一种轻量级的并发模型,类似于协程。2)它们允许开发者手动控制任务的执行流,适合处理I/O密集型任务。3)使用Fibers可以编写更高效、响应性更强的代码。

PHP社区提供了丰富的资源和支持,帮助开发者成长。1)资源包括官方文档、教程、博客和开源项目如Laravel和Symfony。2)支持可以通过StackOverflow、Reddit和Slack频道获得。3)开发动态可以通过关注RFC了解。4)融入社区可以通过积极参与、贡献代码和学习分享来实现。

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

记事本++7.3.1
好用且免费的代码编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

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