验证码生成php
‘壹’ 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代码中登陆界面加一个验证码,如何实现
php登陆页面+验证码的实现,参考如下:
1、首先新建一个php站点;
‘叁’ php如何使用随机函数rand()生成一个数字验证码
参考这个
$code="";
//画布
$image=imagecreatetruecolor(80, 25);
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255));
for($i=0;$i<4;$i++){
$rand_color=imagecolorallocate($image, rand(0,155), rand(0,155), rand(0,155));
$code_tmp=dechex(rand(1,15));
$code.=$code_tmp;
imagestring($image, rand(4,5), rand($i*20,$i*20+20-6), rand(0,13),$code_tmp , $rand_color);
//干扰线
imageline($image, rand($i*20,$i*20+20), rand(0,25), rand($i*20,$i*20+20), rand(0,25), $rand_color);
imageline($image, rand($i*20,$i*20+20), rand(0,25), rand($i*20,$i*20+20), rand(0,25), $rand_color);
}
//保存
session_start();
$_SESSION['yzm']=$code;
session_write_close();
header("content-type:image/png");
imagepng($image);
imagedestroy($image);
‘肆’ 怎样制作PHP验证码
<?php
//验证码:文本类型为图像
header("content-type:image/png");
define('TYPE',3);//1.字母 2.字母数字 3.数字 4.逻辑 5.汉字
session_start();
//创建画布
$img = imagecreatetruecolor(90,33);
//创建颜色
//$bgcolor = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$bgcolor = imagecolorallocate($img,255,255,255);
$textcolor = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
//填充颜色到画布
imagefill($img,0,0,$bgcolor);
//创建但像素的点为干扰项
//for($i=0;$i<100;$i++){
// $pixelcolor = imagecolorallocate($img,rand(150,200),rand(150,200),rand(150,200));
// imagesetpixel($img,rand(0,70),rand(0,30),$pixelcolor);
//}
//
////划线
//$linecolor = imagecolorallocate($img,255,0,0);
//imageline($img,0,0,70,30,$linecolor);
//
////多边形
// $col_poly = imagecolorallocate ( $img , 0 , 255 , 0 );
// imagepolygon ( $img ,
// array (
// 5 , 5,
// 5, 15 ,
// 20,15,
// 20,5
// ),
// 4 ,
// $col_poly );
////弧线
//$arcColor = imagecolorallocate ( $img , 0 , 0 , 255 );
//imagearc($img,35,15,30,30,0,360,$arcColor);
//创建验证码的内容
//#字母
$letter = range('A','Z');
$letterStr = $letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)];
//数字字母
$num = range(0,9);
$numberAndLetter = array_merge($letter,$num);
$nal = $numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)];
//#数字
$number = rand(1000,9999);
//#逻辑
$x = rand(1,9);
$y = rand(1,9);
$expression = $x."+".$y."=?";
$sum = $x+$y;
//#汉字
$CH = array('恭喜发财','财源滚滚','财源广进','才高八斗','学富五车','抬头见喜');
$chstr = $CH[rand(0,count($CH)-1)];
switch(TYPE){
case 1 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$letterStr);$_SESSION['code']=$letterStr;break;
case 2 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$nal);$_SESSION['code']=$nal;break;
case 3 : imagettftext($img,14,0,13,23,$textcolor,'MSYH.TTF',$number);$_SESSION['code']=$number;break;
case 4 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$expression);$_SESSION['code']=$sum;break;
case 5 : imagettftext($img,11,0,6,21,$textcolor,'MSYHBD.TTF',$chstr);$_SESSION['code']=$chstr;break;
}
//输入图像到浏览器
imagepng($img);
?>
‘伍’ PHP如何生成加减算法方式的验证码
<?php
namespace mobile\components;
/**
* @author fenghuo
*
* 改造的加减法验证类
* 使用示例 VerifyCode::get(1,2);
* 验证示例 VerifyCode::check($code);
*/
class VerifyCode
{
/**
* php验证码
*/
public static function get($one,$two,$prefix = '', $font_size = 28)
{
//文件头...
ob_get_clean();
header("Content-type: image/png;charset=utf-8;");
//创建真彩色白纸
$width = $font_size*5;
$height = $font_size+1;
$im = @imagecreatetruecolor($width, $height) or die("建立图像失败");
//获取背景颜色
$background_color = imagecolorallocate($im, 255, 255, 255);
//填充背景颜色
imagefill($im, 0, 0, $background_color);
//获取边框颜色
$border_color = imagecolorallocate($im, 200, 200, 200);
//画矩形,边框颜色200,200,200
imagerectangle($im,0,0,$width - 1, $height - 1,$border_color);
//逐行炫耀背景,全屏用1或0
for($i = 2;$i < $height - 2;$i++) {
//获取随机淡色
$line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
//画线
imageline($im, 2, $i, $width - 1, $i, $line_color);
}
//设置印上去的文字
$firstNum = $one;
$secondNum = $two;
$actionStr = $firstNum > $secondNum ? '-' : '+';
//获取第1个随机文字
$imstr[0]["s"] = $firstNum;
$imstr[0]["x"] = rand(2, 5);
$imstr[0]["y"] = rand(1, 4);
//获取第2个随机文字
$imstr[1]["s"] = $actionStr;
$imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);
$imstr[1]["y"] = rand(1,5);
//获取第3个随机文字
$imstr[2]["s"] = $secondNum;
$imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);
$imstr[2]["y"] = rand(1, 5);
//获取第3个随机文字
$imstr[3]["s"] = '=';
$imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);
$imstr[3]["y"] = 3;
//获取第3个随机文字
$imstr[4]["s"] = '?';
$imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);
$imstr[4]["y"] = 3;
//文字
$text = '';
//写入随机字串
for($i = 0; $i < 5; $i++) {
//获取随机较深颜色
$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));
$text .= $imstr[$i]["s"];
//画文字
imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);
}
session_start();
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum);
//显示图片
ImagePng($im);
//销毁图片
ImageDestroy($im);
}
public static function check($code)
{
if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) {
return true;
} else {
return false;
}
}