❶ php怎么实现验证码的
验证码功能机制实现思路
常规的验证码实现:
a、产生一张png的图片
b、为图片设置背景色
c、设置字体颜色和样式
d、产生4位数的随机的验证码
e、把产生的每个字符调整旋转角度和位置画到png图片上
f、加入噪点和干扰线防止注册机器分析原图片来恶意注册
g、输出图片
h、释放图片所占内存
i、将验证码保存到session或是数据库
j、将和输入的验证码进行对比
短信(邮箱)验证码机制:
a、产生4-6位数的随机的验证码
b、把产生的每个字符保存到session或是数据库
c、将验证码发送到用户的手机(邮箱)
d、用户在规定时间内进行输入
e、将验证码从session或是数据库中取出
f、将和输入的验证码进行对比验证
❷ 求 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生成验证码
PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:
1、产生一张png的图片;
2、为图片设置背景色;
3、设置字体颜色和样式;
4、产生4位数的随机的验证码;
5、把产生的每个字符调整旋转角度和位置画到png图片上;
6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;
7、输出图片;
8、释放图片所占内存。
session_start();
getCode(4,60,20);
functiongetCode($num,$w,$h){
$code="";
for($i=0;$i<$num;$i++){
$code.=rand(0,9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证时用
$_SESSION["helloweba_num"]=$code;
//创建图片,定义颜色值
header("Content-type:image/PNG");
$im=imagecreate($w,$h);
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
$bgcolor=imagecolorallocate($im,255,255,255);
//填充背景
imagefill($im,0,0,$gray);
//画边框
imagerectangle($im,0,0,$w-1,$h-1,$black);
//随机绘制两条虚线,起干扰作用
$style=array($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im,$style);
$y1=rand(0,$h);
$y2=rand(0,$h);
$y3=rand(0,$h);
$y4=rand(0,$h);
imageline($im,0,$y1,$w,$y3,IMG_COLOR_STYLED);
imageline($im,0,$y2,$w,$y4,IMG_COLOR_STYLED);
//在画布上随机生成大量黑点,起干扰作用;
for($i=0;$i<80;$i++){
imagesetpixel($im,rand(0,$w),rand(0,$h),$black);
}
//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx=rand(3,8);
for($i=0;$i<$num;$i++){
$strpos=rand(1,6);
imagestring($im,5,$strx,$strpos,substr($code,$i,1),$black);
$strx+=rand(8,12);
}
imagepng($im);//输出图片
imagedestroy($im);//释放图片所占内存
}
❹ 那位高人能告诉小女在用php GD库做验证码的时候怎样加入干扰线啊,注意不是直线啊,是那种曲线啊
这段代码你可以看看,自己原创的哦,
<?php
$im = ImageCreate(200,200);
$red = ImageColorAllocate($im,0xFF,0x00,0x00);
$black = ImageColorAllocate($im,0x00,0x00,0x00);
$white = ImageColorAllocate($im,0xFF,0xFF,0xFF);
$ys1 = ImageColorAllocate($im,0xE9,0xB1,0x50);
$ys2 = ImageColorAllocate($im,0x98,0x25,0xCB);
$ys3 = ImageColorAllocate($im,0x40,0x88,0x47);
ImageFilledRectangle($im,50,50,150,150,$black);
//点
for($i=0;$i<300;$i++){
ImageSetPixel($im,rand(1,200),rand(1,200),$white);
}
//虚线
for($i=0;$i<10;$i++){
ImageDashedLine($im,rand(1,200),rand(1,200),rand(1,200),rand(1,200),$ys1);
}
//线
for($i=0;$i<10;$i++){
ImageLine($im,rand(1,200),rand(1,200),rand(1,200),rand(1,200),$ys1);
}
//矩形框
for($i=0;$i<3;$i++){
ImageRectangle($im,rand(1,200),rand(1,200),rand(1,200),rand(1,200),$ys1);
}
//矩形面
for($i=0;$i<2;$i++){
ImageFilledRectangle($im,rand(1,200),rand(1,200),rand(1,200),rand(1,200),$ys1);
}
//多边形
$point = array(rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200));
ImagePolygon($im,$point,count($point)/2,$ys2);
ImageFilledPolygon($im,$point,count($point)/2,$ys2);
//弧线
ImageArc($im,rand(20,180),rand(20,180),rand(50,150),rand(50,150),rand(0,360),rand(0,360),$ys3);
//打字
ImageString($im,4,20,30,"add word",$ys3);
//ImageTTFText($im,30,0,rand(0,50),rand(30,200),$ys3,'msyhbd.ttf',"添加文字");
header('Content-Type:image/png');
ImagePNG($im);
?>
❺ 写了一个php验证码,单独运行可以,引用就显示错误
你应该这样用:
checkcode.php:
<?php
//画出验证码
$checkCode="";
for($i=0;$i<4;$i++){
$checkCode.=dechex(rand(1,15));
}
//创建画布
$image=imagecreatetruecolor(110,30);
//存入session
session_start();
$_SESSION['checkcode']=$checkCode;
//设置颜色
$red=imagecolorallocate($image,255,255,255);
//画出干扰线
for($i=0;$i<20;$i++){
imageline($image,rand(0,110),rand(0,110),rand(0,30),rand(0,30),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)));
}
imagestring($image,rand(5,8),rand(0,60),rand(0,10),$checkCode,$red);
//输出到页面
ob_clean();
header("content-type:image/png");
imagepng($image);
//销毁图片
imagedestroy($image);
?>
test.php:
显示验证码<imgsrc="checkcode.php"/>
❻ PHP图形验证码识别
<?php
Header("Content-type: image/gif");
/*
* 初始化
*/
$border = 0; //是否要边框 1要:0不要
$how = 4; //验证码位数
$w = $how*15; //图片宽度
$h = 20; //图片高度
$fontsize = 5; //字体大小
$alpha = "abcdefghijkmnopqrstuvwxyz"; //验证码内容1:字母
$number = "023456789"; //验证码内容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($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($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
//session_start();
//$_SESSION['randcode'] = $randcode;
//把验证码字符写入COOKIE
setcookie( "randcode", $randcode, (time() + 3600*24*30), "/" );
/*绘图结束*/
Imagegif($im);
ImageDestroy($im);
/*绘图结束*/
?>
❼ php验证码怎么在前from表单验证呢请大师请教,谢谢
把验证码的值当作一个参数存在html表单/ajax表单中(如,以vcode命名增加一个hiden text);
在控制器接收表单的逻辑,接收$_POST['vcode'],判断$_POST['vcode']是不是生成的验证码,是,跳过,不是返回验证码错误到表单页面;
其实验证就类似接口一样,双方约定规则,统一密钥,而验证只是你自己去验证罢了
❽ 我的php代码中登陆界面加一个验证码,如何实现
php登陆页面+验证码的实现,参考如下:
1、首先新建一个php站点;
❾ php中如何使用随机函数rand()生成一个数字验证码
如果要生成四位数字的验证码,则可以用函数:
$srand = rand(1000,9999);
会生成在1000到9999之间的随机数字,如果要生成更多位数的数字,可以更改最小、最大值。
❿ php 下面的验证码怎么做
干扰像素其实就是一定算法生成的随机点。这个不会每张图片都一样的,那样就有规律可循了,容易被破解。