Ⅰ php如何实现验证码许昌鲤鱼IT计算机电脑软件编程培训中心
验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码。好吧,其实是没有事情干,但是又不想浪费时间,所以学习了下php实现验证码。正所谓,技多不压身。而且,也可以封装成一个函数,以后使用的时候也是很方便的,当然现在未封装。
现在来说说简单的纯数字验证码吧。
如果是初学者,建议按照我代码的注释 //数字 一步步来。最简单的方法,还是把整个代码复制走了。
新建一个captcha.php:
php //10>设置session,必须处于脚本最顶部
session_start(); $image = imagecreatetruecolor(100, 30); //1>设置验证码图片大小的函数
//5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色
imagefill($image, 0, 0, $bgcolor); //10>设置变量
$captcha_code = ""; //7>生成随机数字
for($i=0;$i<4;$i++){ //设置字体大小
$fontsize = 6;
//设置字体颜色,随机颜色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色
//设置数字
$fontcontent = rand(0,9); //10>.=连续定义变量
$captcha_code .= $fontcontent;
//设置坐标
$x = ($i*100/4)+rand(5,10); $y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
} //10>存到session
$_SESSION['authcode'] = $captcha_code; //8>增加干扰元素,设置雪花点
for($i=0;$i<200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
//imagesetpixel — 画一个单一像素
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
} //9>增加干扰元素,设置横线
for($i=0;$i<4;$i++){ //设置线的颜色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线
imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
} //2>设置头部,image/png
header('Content-Type: image/png'); //3>imagepng() 建立png图形函数
imagepng($image); //4>imagedestroy() 结束图形函数 销毁$image
imagedestroy($image);
接着就是静态页的代码了:index.html
doctype html><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>确认验证码title>
head>
<body>
<form method="post" action="./form.php">
<p>验证码: <img id="captcha_img" border='1' src='./captcha.php?r=echo rand(); ?>' style="width:100px; height:30px" /> <a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">换一个?a>
p>
<P>请输入验证码:<input type="text" name='authcode' value=''/>p>
<p><input type='submit' value='提交' style='padding:6px 5px;'/>p>
body>html>
从index.html可以看到,提交的表单是到form.php的,所以还要有一个判断的form.php代码:
php header("Content-Type:text/html;charset=utf-8"); //设置头部信息
//isset()检测变量是否设置
if(isset($_REQUEST['authcode'])){ session_start(); //strtolower()小写函数
if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){ //跳转页面
echo "<script language=\"javascript\">"; echo "document.location=\"./form.php\""; echo "</script>";
}else{ //提示以及跳转页面
echo "<script language=\"javascript\">"; echo "alert('输入错误!');"; echo "document.location=\"./form.php\""; echo "</script>";
} exit();
}
那么,纯数字的实现了,数字加英文的也应该不难了。要修改的代码 只是在 captcha.php 将 //7>生成随机数字 修改成 //7>生成随机的字母和数字,如果你真的很可爱的就修改这几个字就认为可以实现的话,那么祝贺你,你永远保持快乐。脑残儿童欢乐多。
废话不多说了,拉代码吧。
php //10>设置session,必须处于脚本最顶部
session_start(); $image = imagecreatetruecolor(100, 30); //1>设置验证码图片大小的函数
//5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色
imagefill($image, 0, 0, $bgcolor); //10>设置变量
$captcha_code = ""; //7>生成随机的字母和数字
for($i=0;$i<4;$i++){ //设置字体大小
$fontsize = 8;
//设置字体颜色,随机颜色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色
//设置需要随机取的值,去掉容易出错的值如0和o
$data =''; //取出值,字符串截取方法 strlen获取字符串长度
$fontcontent = substr($data, rand(0,strlen($data)),1); //10>.=连续定义变量
$captcha_code .= $fontcontent;
//设置坐标
$x = ($i*100/4)+rand(5,10); $y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
} //10>存到session
$_SESSION['authcode'] = $captcha_code; //8>增加干扰元素,设置雪花点
for($i=0;$i<200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
//imagesetpixel — 画一个单一像素
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
} //9>增加干扰元素,设置横线
for($i=0;$i<4;$i++){ //设置线的颜色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线
imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
} //2>设置头部,image/png
header('Content-Type: image/png'); //3>imagepng() 建立png图形函数
imagepng($image); //4>imagedestroy() 结束图形函数 销毁$image
imagedestroy($image);
其他的两个页面,不许要修改。
一般而言,现在就已经够用了。但是就像动漫一样,总会有番外。
那么,我们来个汉字的番外吧。其实我也准备将汉字的验证码放到我的毕业设计里面,虽然现在很流行滑动验证码,但是本人毕竟不是专门学习js的。
而且,还可以和答辩的老师说,我们验证码不需要素材,连图片也是生成的,用自己的知识装13,也没有设么的。
php //11>设置session,必须处于脚本最顶部
session_start(); //1>设置验证码图片大小的函数
$image = imagecreatetruecolor(200, 60);
//5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色
imagefill($image, 0, 0, $bgcolor); //7>设置ttf字体
$fontface = 'FZYTK.TTF'; //7>设置字库,实现简单的数字储备
$str='天地不仁以万物为刍狗圣人不仁以百姓为刍狗这句经常出现在控诉暴君暴政上地残暴不仁把万物都当成低贱的猪狗来看待而那些高高在上的所谓圣人们也没两样还不是把我们老百姓也当成猪狗不如的东西但实在正取的解读是地不情感用事对万物一视同仁圣人不情感用事对百姓一视同仁执子之手与子偕老当男女主人公含情脉脉看着对方说了句执子之手与子偕老女方泪眼朦胧含羞地回一句讨厌啦这样的情节我们是不是见过很多但是我们来看看这句的原句死生契阔与子成说执子之手与子偕老于嗟阔兮不我活兮于嗟洵兮不我信兮意思是说战士之间的约定说要一起死现在和我约定的人都走了我怎么活啊赤裸裸的兄弟江湖战友友谊啊形容好基友的基情比男女之间的爱情要合适很多吧'; //str_split()切割字符串为一个数组,一个中文在utf_8为3个字符
$strdb = str_split($str,3);
//>11
$captcha_code = ''; //8>生成随机的汉子
for($i=0;$i<4;$i++){ //设置字体颜色,随机颜色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色
//随机选取中文
$in = rand(0,count($strdb)); $cn = $strdb[$in]; //将中文记录到将保存到session的字符串中
$captcha_code .= $cn; /*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,
string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐标,颜色,字体路径,文本字符串
mt_rand()生成更好的随机数,比rand()快四倍*/
imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn);
} //11>存到session
$_SESSION['authcode'] = $captcha_code; //9>增加干扰元素,设置点
for($i=0;$i<200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
//imagesetpixel — 画一个单一像素
imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);
} //10>增加干扰元素,设置线
for($i=0;$i<4;$i++){ //设置线的颜色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线
imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor);
} //2>设置头部,image/png
header('Content-Type: image/png'); //3>imagepng() 建立png图形函数
imagepng($image); //4>imagedestroy() 结束图形函数 销毁$image
imagedestroy($image);
其他的页面也是不需要修改的。
效果图如下:
Ⅱ 像这样的验证码有没有办法用 PHP 识别
一般没办法 只能用一些办法来搞 比如curl 把图片存下来 然后用txt寸你看到的验证码 然后PHP读取txt内容 或者你用一些打码平台!
Ⅲ php图片验证码实现
可以用php的GD库做
//随机生成验证码
class randomString
{
function createRandomStr($strLen)
{
list($usec, $sec) = explode(' ', microtime());
(float) $sec + ((float) $usec * 100000);
$number = '';
$number_len = $strLen;
$stuff = '';//附加码显示范围ABCDEFGHIJKLMNOPQRSTUVWXYZ
$stuff_len = strlen($stuff) - 1;
for ($i = 0; $i < $number_len; $i++) {
$number .= substr($stuff, mt_rand(0, $stuff_len), 1);
}
return $number;
}
}
通过ZD库将验证码变成图片
$number = $createStr->createRandomStr('4');//验证码的位数
$number_len = strlen($number);
$_SESSION["VERIFY_CODE"] = $number;
// 生成验证码图片
$img_width = 60;
$img_height = 20;
$img = imageCreate($img_width, $img_height);
ImageColorAllocate($img, 0x6C, 0x74, 0x70);
$white = ImageColorAllocate($img, 0xff, 0xff, 0xff);
$ix = 6;
$iy = 2;
for ($i = 0; $i < $number_len; $i++) {
imageString($img, 5, $ix, $iy, $number[$i], $white);
$ix += 14;
}
for($i=0;$i<200;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);
}
// 输出图片
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($img);
imagedestroy($img);
Ⅳ PHP写的图形验证码乱码
我吧我的给你用吧
class wxhVerify {
static function imageVerify($length=4, $mode=1, $width=48, $height=22, $verifyName='verify') {
$randval = randomString($length, $mode);
$_SESSION[$verifyName] = md5($randval);
$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
$im = @imagecreatetruecolor($width, $height);
$r = Array(225, 255, 255, 223);
$g = Array(225, 236, 237, 255);
$b = Array(225, 236, 166, 125);
$key = mt_rand(0, 3);
//随机背景色
$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);
//边框色
$borderColor = imagecolorallocate($im, 100, 100, 100);
//点颜色
$pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
//干扰
for ($i = 0; $i < 10; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for ($i = 0; $i < 25; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
}
for ($i = 0; $i < $length; $i++) {
imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
exit;
}
}
//生成随机字符串
function randomString($len=6, $type=1, $addChars='') {
switch ($type) {
case 0:
$chars = '' . $addChars;
break;
case 1:
$chars = str_repeat('0123456789', 3);
break;
case 2:
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
break;
case 3:
$chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
break;
default :
//默认去掉了容易混淆的字符oOLl和数字01,要增加请使用addChars参数
$chars = '' . $addChars;
break;
}
if ($len > 10) {
//位数过长重复字符串一定次数
$chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5);
}
$chars = str_shuffle($chars);
return substr($chars, 0, $len);
}
调用 wxhVerify::imageVerify();
Ⅳ php怎么识别验证码
使用图片验证码,一般注册机很难识别图片上的字符。 图片验证码原理:在注册表单页上,添加一个表单项,用于提交用户看到的图片验证码,在适合位置放一个
Ⅵ 求 PHP 图片验证码类 给出详细调用方法 谢谢!!!
[code.php]
<?php
/**
*验证码图片
*/
session_start();
Header("Content-type:image/gif");
/*
*初始化
*/
$border=0;//是否要边框1要:0不要
$how=4;//验证码位数
$w=$how*15;//图片宽度
$h=20;//图片高度
$fontsize=10;//字体大小
$alpha="abcdefghijkmnpqrstuvwxyz";//验证码内容1:字母
$number="23456789";//验证码内容2:数字
$randcode="";//验证码字符串初始化
srand((double)microtime()*1000000);//初始化随机数种子
$im=ImageCreate($w,$h);//创建验证图片
/*
*绘制基本框架
*/
$bgcolor=ImageColorAllocate($im,255,255,255);//设置背景颜色
ImageFill($im,0,0,$bgcolor);//填充背景色
if($border)
{
$black=ImageColorAllocate($im,0,0,0);//设置边框颜色
ImageRectangle($im,0,0,$w-1,$h-1,$black);//绘制边框
}
/*
*逐位产生随机字符
*/
for($i=0;$i<$how;$i++)
{
$alpha_or_number=mt_rand(0,1);//字母还是数字
$str=$alpha_or_number?$alpha:$number;
$which=mt_rand(0,strlen($str)-1);//取哪个字符
$code=substr($str,$which,1);//取字符
$j=!$i?4:$j+15;//绘字符位置
$color3=ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));//字符随即颜色
ImageChar($im,$fontsize,$j,3,$code,$color3);//绘字符
$randcode.=$code;//逐位加入验证码字符串
}
/*
*如果需要添加干扰就将注释去掉
*
*以下for()循环为绘背景干扰线代码
*/
/*+-------------------------------绘背景干扰线开始--------------------------------------------+*/
for($i=0;$i<5;$i++)//绘背景干扰线
{
$color1=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干扰线颜色
ImageArc($im,mt_rand(-5,$w),mt_rand(-5,$h),mt_rand(20,300),mt_rand(20,200),55,44,$color1);//干扰线
}
/*+-------------------------------绘背景干扰线结束--------------------------------------+*/
/*
*如果需要添加干扰就将注释去掉
*
*以下for()循环为绘背景干扰点代码
*/
/*+--------------------------------绘背景干扰点开始------------------------------------------+*/
/*
for($i=0;$i<$how*40;$i++)//绘背景干扰点
{
$color2=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干扰点颜色
ImageSetPixel($im,mt_rand(0,$w),mt_rand(0,$h),$color2);//干扰点
}
*/
/*+--------------------------------绘背景干扰点结束------------------------------------------+*/
//把验证码字符串写入session方便提交登录信息时检验验证码是否正确例如:$_POST['randcode']=$_SESSION['randcode']
$_SESSION['randcode']=$randcode;
/*绘图结束*/
Imagegif($im);
ImageDestroy($im);
/*绘图结束*/
?>
[调用方法]
<SCRIPTLANGUAGE="JavaScript">
<!--
functionreloadcode(){
vard=newDate();
document.getElementById('safecode').src="/code.php?t="+d.toTimeString()
}
//-->
</SCRIPT>
验证码:<inputname="chknumber"type="text"maxlength="4"class="chknumber_input"/><imgsrc='code.php'id="safecode"onclick="reloadcode()"title="看不清楚?点击切换!"></img>
Ⅶ php的图片验证码代码
这个是phpcms的验证码,经过十几万个网站经验的,非常好用
<?php
session_start();
$enablegd = 1;
//判断图像处理函数是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}
ob_clean(); //清理缓冲
if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //获取$consts中的一个随机数
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //获取$vowels中的一个随机数
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //显示4个字符
$imageX = strlen($radomstring)*8; //图像的宽
$imageY = 20; //图像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一个真彩色图像
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
);
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配颜色并说明透明度
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中间背景
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中间背景2
//与左上角的颜色相同的都会被填充
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//往图像上写入文字
imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.ttf', $string[3]);
//画边框
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
//画一些随机出现的点
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//画随机出现的线
for ($x=0; $x<9;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //画线
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //画椭圆
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png\r\n");
imagepng($im);
imagedestroy($im);
}
else
{
$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');
if(!is_array($files)) die('请检查文件目录完整性:/images/checkcode/');
$checkcodefile = $files[rand(0, count($files)-1)]; //随机其中一个文件
$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //获得文件名
header("content-type:image/jpeg\r\n");
include $checkcodefile;
}
?>
Ⅷ 如何用PHP写图片验证码验证代码
你去php100看看视频教程吧,踏踏实实的看完了,你的问题也就解决了