登录  /  注册

php验证码怎么做?

黄舟
发布: 2017-08-03 13:47:21
原创
2082人浏览过

php验证码制作是对php基本功的考核,php验证码制作必需开启gd库,因为要用到gd库里面的不少函数

PVVZM{8)@3Q2NHMVA]V5195.png

推荐学习PHP开发验证码教程

1.创建验证码底图

<?php
$image = imagecreatetruecolor(100,30);
$bgcolor = imagecolorallocate($image,000,255,255);//#FFFFFFFFFFFF
imagefill($image,0,0,$bgcolor);

header(&#39;content-type: image/png&#39;);
imagepng($image);
//销毁
imagedestroy($image);
?>
登录后复制

课程链接:http://www.php.cn/code/3872.html

2.实现数字验证码

<?php
$image = imagecreatetruecolor(100,30);
$bgcolor = imagecolorallocate($image,255,255,255);//#FFFFFFFFFFFF
imagefill($image,0,0,$bgcolor);
for ($i=0;$i<4;$i++){
 $fontsize = 6;
 $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
 $fontcontent = rand(0,9);
 $x = ($i * 100/4)+rand(5,10);
 $y = rand(5,10);
 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
header(&#39;content-type: image/png&#39;);
imagepng($image);
//销毁
imagedestroy($image);

?>
登录后复制

课程链接:http://www.php.cn/code/3874.html

3.增加干扰元素

<?php
$image = imagecreatetruecolor(100,30);
$bgcolor = imagecolorallocate($image,255,255,255);//#FFFFFFFFFFFF
imagefill($image,0,0,$bgcolor);
for ($i=0;$i<4;$i++){
 $fontsize = 6;
 $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
 $fontcontent = rand(0,9);
 $x = ($i * 100/4)+rand(5,10);
 $y = rand(5,10);
 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
for($i=0;$i<200;$i++){
 $pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
 imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
header(&#39;content-type: image/png&#39;);
imagepng($image);
//销毁
imagedestroy($image);
?>
登录后复制

课程链接:http://www.php.cn/code/3875.html

4.字母和数字混合验证码

<?php
$image = imagecreatetruecolor(100,30);
$bgcolor = imagecolorallocate($image,255,255,255);//#FFFFFFFFFFFF
imagefill($image,0,0,$bgcolor);
for ($i=0;$i<4;$i++){
 $fontsize = 6;
 $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
 $data=&#39;abcdefghijklmnopqrstuvwxyz1234567890&#39;;
 $fontcontent=substr($data,rand(0,strlen($data)),1);
 $x = ($i * 100/4)+rand(5,10);
 $y = rand(5,10);
 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
for($i=0;$i<200;$i++){
 $pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
 imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
for($i=0;$i<8;$i++){
 $linecolor = imagecolorallocate($image,rand(60,220),rand(60,220),rand(60,220));
 imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
header(&#39;content-type: image/png&#39;);
imagepng($image);
//销毁
imagedestroy($image);

?>
登录后复制

课程链接:http://www.php.cn/code/3878.html

5.使用session存储验证信息

<?php
session_start();
$image = imagecreatetruecolor(100,30);
$bgcolor = imagecolorallocate($image,255,255,255);//#FFFFFFFFFFFF
imagefill($image,0,0,$bgcolor);
$captch_code="";
for ($i=0;$i<4;$i++){
 $fontsize = 6;
 $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
 $data=&#39;abcdefghijklmnopqrstuvwxyz1234567890&#39;;
 $fontcontent=substr($data,rand(0,strlen($data)),1);
 $captch_code.="$fontcontent";
 $x = ($i * 100/4)+rand(5,10);
 $y = rand(5,10);
 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION[&#39;code&#39;]=$captch_code;
for($i=0;$i<200;$i++){
 $pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
 imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
for($i=0;$i<8;$i++){
 $linecolor = imagecolorallocate($image,rand(60,220),rand(60,220),rand(60,220));
 imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
header(&#39;content-type: image/png&#39;);
imagepng($image);
//销毁
imagedestroy($image);
?>
登录后复制

课程链接:http://www.php.cn/code/3879.html

6.验证码的使用

<?php
session_start();
$image = imagecreatetruecolor(100,30);
$bgcolor = imagecolorallocate($image,255,255,255);//#FFFFFFFFFFFF
imagefill($image,0,0,$bgcolor);
$captch_code="";
for ($i=0;$i<4;$i++){
 $fontsize = 6;
 $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
 $data=&#39;abcdefghijklmnopqrstuvwxyz1234567890&#39;;
 $fontcontent=substr($data,rand(0,strlen($data)),1);
 $captch_code.="$fontcontent";
 $x = ($i * 100/4)+rand(5,10);
 $y = rand(5,10);
 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION[&#39;code&#39;]=$captch_code;
for($i=0;$i<200;$i++){
 $pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
 imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
for($i=0;$i<5;$i++){
 $linecolor = imagecolorallocate($image,rand(60,220),rand(60,220),rand(60,220));
 imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
header(&#39;content-type: image/png&#39;);
imagepng($image);
//销毁
imagedestroy($image);
?>
登录后复制

课程链接:http://www.php.cn/code/4832.html

以上就是php验证码怎么做?的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号