• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP实现汉字验证码

php 搞代码 3年前 (2022-01-22) 33次浏览 已收录 0个评论

本篇文章主要介绍PHP实现汉字验证码,感兴趣的朋友参考下,希望对大家有所帮助。

现在越来越多的网站都开始使用汉字验证码了,既增加了我们国人的亲切感,同时也增加了机器破解的难度,这里我就简单粗暴的说一下。。。

创建背景画布

$image = imagecreatetruecolor(200, 60);$background = imagecolorallocate($image, 255, 255, 255);imagefill($image, 0, 0, $background);

画干扰点

for ($i=0; $i < 300; $i++) {   $pixColor = imagecolorallocate($image, rand(150, 240), rand(150, 240), rand(150, 240));  $pixX = rand(10, 190);  $pixY = rand(5, 55);  imagesetpixel($image, $pixX, $pixY, $pixColor);}

画干扰线

//4条水平线for ($i=0; $i < 5; $i++) {   $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));  $lineX1 = 0;  $lineX2 = 300;  $lineY1 = ($i + 1) * 12;  $lineY2 = ($i + 1) * 12;  imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);}//10条垂直线for ($i=0; $i < 30; $i++) {   $l<strong>*本文来@源gao@daima#com搞(%代@#码@网2</strong><pre>搞代gaodaima码

ineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150)); $lineX1 = ($i + 1) * 10; $lineX2 = ($i + 1) * 10; $lineY1 = 0; $lineY2 = 60; imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);}

画汉字

$text = array('栀', '子', '花', '开');for ($i=0; $i < 4; $i++) {  $textColor = imagecolorallocate($image, rand(20, 100), rand(20, 100), rand(20, 100));  $textX = $i * 50 + 10;  $textY = rand(40, 60);  imagettftext($image, 30, rand(20, 50), $textX, $textY, $textColor, "/Library/Fonts/华文仿宋.ttf", $text[$i]);}

这里注意一下,字体文件一定要支持中文的

编码要使用utf-8,gbk的中文记得要转吗【iconv函数可以帮助你】

输出图像

header("Content-Type:image/png");imagepng($image);

销毁资源

imagedestroy($image);

经过粗略的搞吧搞吧,中文验证码也就显示出来了,当然一般网站使用的时候会有一个汉字库种子,从里面随机取出特定个数的汉字显示,最后就是记录到session进行验证了。

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php设计模式基础知识与应用

php图片文件操作封装类

PHP中那些经常混淆的知识点

以上就是PHP实现汉字验证码的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP实现汉字验证码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址