导航:首页 > 编程语言 > php实现图片验证码

php实现图片验证码

发布时间:2023-01-08 04:12:18

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的图片验证码代码

这个是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 图片验证码类 给出详细调用方法 谢谢!!!

[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怎么实现验证码的

验证码功能机制实现思路

  1. 常规的验证码实现:

    a、产生一张png的图片


    b、为图片设置背景色


    c、设置字体颜色和样式


    d、产生4位数的随机的验证码


    e、把产生的每个字符调整旋转角度和位置画到png图片上


    f、加入噪点和干扰线防止注册机器分析原图片来恶意注册


    g、输出图片


    h、释放图片所占内存


    i、将验证码保存到session或是数据库


    j、将和输入的验证码进行对比

  2. 短信(邮箱)验证码机制:

    a、产生4-6位数的随机的验证码


    b、把产生的每个字符保存到session或是数据库


    c、将验证码发送到用户的手机(邮箱)


    d、用户在规定时间内进行输入


    e、将验证码从session或是数据库中取出


    f、将和输入的验证码进行对比验证

❺ 求助!!在php中想实现图片验证码的效果

1.rand一个数串。
2.存入cookie中或session中。
3.将数用gd库方法生成图片。
4.用户输入后与cookie或session对比。
done

❻ php验证码怎么实现

1.新建code.php验证码生成文件

在此之前必须打开php的GD库,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分号。代码如下:

<?php

session_start();

//生成验证码图片

Header("Content-type: image/PNG");

$im = imagecreate(44,18);

$back = ImageColorAllocate($im, 245,245,245);

imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);

//生成4位数字

for($i=0;$i<4;$i++){

$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));

$authnum=rand(1,9);

$vcodes.=$authnum;

imagestring($im, 5, 2+$i*10, 1, $authnum, $font);

}

for($i=0;$i<100;$i++) //加入干扰象素

{

$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im, rand()p , rand()0 , $randcolor);

}

ImagePNG($im);

ImageDestroy($im);

$_SESSION['Checknum'] = $vcodes;

?>

2. 显示验证码图片

在需要显示验证码的页面中加入

<input type="text" name="passcode" >

<img src="code.php">

3.判断并获取验证码的值

验证码是通过第一步骤代码中的$_SESSION['Checknum'] = $vcodes;赋的值,所以验证码的值存在$_SESSION['Checknum']当中。在验证页面,使用以下代码,

...

session_start();//启动会话

$code=$_POST["passcode"];

if( $code == $_SESSION["Checknum"])

{...}即可完成验证码登录。

运行截图:

望采纳,谢谢

❼ 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 绘制网站登录首页图片验证码

几乎所有的网站登录页都会有验证码,验证码是一种安全保护机制,在注册时要求必须有人工操作进行验证,用于防止垃圾注册机大量注册用户账号占用服务器内存从而使服务器瘫痪。
图片验证码的实现十分简单。首先从指定字符集合中随机抽取固定数目的字符,以一种不规则的方法画在画布上,再适当添加一些干扰点和干扰元素,最后将图片输出,一张崭新的验证码就完成了。
先给大家展示下生成的验证码:

点击刷新:

如果大家对实现效果非常满意,请继续往下看。
前端代码如下:
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="content-type"
content="text/html;charset=utf-8">
<title>This
is
a
test!</title>
<link
rel="stylesheet"
type="text/css"
href="css/bootstrap.min.css">
</head>
<body>
<form
name="form">
<input
type="text"
placeholder="账号"/><br/>
<input
type="password"
placeholder="密码"/><br/>
<input
type="text"
placeholder="验证码"/>
<img
id="verImg"
src="libs/verification.php"/>
<a
href="#"
class="change"
onclick="changeVer()">点击刷新</a><br/>
<input
type="submit"
value="登录"/>
</form>
<script
type="text/javascript">
//刷新验证码
function
changeVer(){
document.getElementById("verImg").src="libs/verification.php?tmp="+Math.random();
}
</script>
</body>
</html>
php脚本文件验证码的代码如下:
<?php
session_start();
//开启session记录验证码数据
vCode(4,
15);//设置验证码的字符个数和图片基础宽度
//vCode
字符数目,字体大小,图片宽度、高度
function
vCode($num
=
4,
$size
=
20,
$width
=
0,
$height
=
0)
{
!$width
&&
$width
=
$num
*
$size
*
4
/
5
+
15;
!$height
&&
$height
=
$size
+
10;
//设置验证码字符集合
$str
=
"";
//保存获取的验证码
$code
=
''
//随机选取字符
for
($i
=
0;
$i
<
$num;
$i++)
{
$code
.=
$str[mt_rand(0,
strlen($str)-1)];
}
//创建验证码画布
$im
=
imagecreatetruecolor($width,
$height);
//背景色
$back_color
=
imagecolorallocate($im,
mt_rand(0,100),mt_rand(0,100),
mt_rand(0,100));
//文本色
$text_color
=
imagecolorallocate($im,
mt_rand(100,
255),
mt_rand(100,
255),
mt_rand(100,
255));
imagefilledrectangle($im,
0,
0,
$width,
$height,
$back_color);
//
画干扰线
for($i
=
0;$i
<
5;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagearc($im,
mt_rand(-
$width,
$width),
mt_rand(-
$height,
$height),
mt_rand(30,
$width
*
2),
mt_rand(20,
$height
*
2),
mt_rand(0,
360),
mt_rand(0,
360),
$font_color);
}
//
画干扰点
for($i
=
0;$i
<
50;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagesetpixel($im,
mt_rand(0,
$width),
mt_rand(0,
$height),
$font_color);
}
//随机旋转角度数组
$array=array(5,4,3,2,1,0,-1,-2,-3,-4,-5);
//
输出验证码
//
imagefttext(image,
size,
angle,
x,
y,
color,
fontfile,
text)
@imagefttext($im,
$size
,
array_rand($array),
12,
$size
+
6,
$text_color,
'c:WINDOWSFontssimsun.ttc',
$code);
$_SESSION["VerifyCode"]=$code;
//no-cache在每次请求时都会访问服务器
//max-age在请求1s后再次请求会再次访问服务器,must-revalidate则第一发送请求会访问服务器,之后不会再访问服务器
//
header("Cache-Control:
max-age=1,
s-maxage=1,
no-cache,
must-revalidate");
header("Cache-Control:
no-cache");
header("Content-type:
image/png;charset=gb2312");
//将图片转化为png格式
imagepng($im);
imagedestroy($im);
}
?>
好了,关于小编给大家介绍的php绘制图片验证就给大家介绍这么多,希望对大家有所帮助!

阅读全文

与php实现图片验证码相关的资料

热点内容
对外汉语pdf 浏览:514
如何用mamp本地web服务器 浏览:869
如何加密自己js代码 浏览:623
排列组合a与c的算法 浏览:534
如何在文件夹中找到同名内容 浏览:786
有什么app文字转韩文配音 浏览:372
循环宏1命令 浏览:35
斐波那契数列矩阵算法 浏览:674
公式保护后加密不了 浏览:82
java跳转到jsp 浏览:819
327平方根算法 浏览:216
win7美化命令行终端 浏览:797
免加密狗图片 浏览:485
一只透明的鸟是什么app 浏览:817
空气压缩机油批发商 浏览:69
linuxifexist 浏览:4
加密tf卡拷入文件 浏览:399
山西php工资 浏览:673
福州看病预约用什么小程序app 浏览:238
php保留两位小数不四舍五入 浏览:292