php经典实例验证码的生成和使用源码免费下载

来源:https://www.sucaihuo.com/php/946.html 2016-09-23 15:09浏览(1853) 收藏

php经典实例验证码的生成和使用源码免费下载
php经典实例验证码的生成和使用源码免费下载
分类:表单代码 > 验证码 难易:入门级
下载资源 下载积分: 26 积分
<html>
	<head>
		<title>验证码的使用<title>
	</head>
	<body>
		<h2>用户登录(验证码的使用)</h2>
		<form action="" method="post">
		账号:<input type="text" name="user"/><br/><br/>
		密码:<input type="password" name="user"/><br/><br/>
		验证:<input type="text" size="6" name="user"/>
				<img src="code.php" onclick="this.src='code.php?id='+Math.random()"/> <br/><br/>
		
		<input type="submit" value="用户登录"/>
		</form>
	</body>
</html>
<?php
//绘制验证码(生成)

$num=4; //验证码的长度
$str = getCode($num,0);// 使用下面的自定义函数,获取需要的验证码值

//1. 创建一个画布、分配颜色
$width=$num*20;//宽度
$height=30;//高度
$im = imagecreatetruecolor($width,$height);//创建一个画布
//定义几个颜色(输出不同颜色的验证码)
$color[] = imagecolorallocate($im,111,0,55);
$color[] = imagecolorallocate($im,0,77,0);
$color[] = imagecolorallocate($im,0,0,160);
$color[] = imagecolorallocate($im,221,111,0);
$color[] = imagecolorallocate($im,220,0,0);
$bg = imagecolorallocate($im,240,240,240);//背景
//2. 开始绘画
 imagefill($im,0,0,$bg);
 imagerectangle($im,0,0,$width-1,$height-1,$color[rand(0,4)]);
  
 //随机添加干扰点
 for($i=0;$i<200;$i++){
	$c = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//随机一个颜色
	imagesetpixel($im,rand(0,$width),rand(0,$height),$c);
 }
 
 //随机添加干扰线
 for($i=0;$i<5;$i++){
	$c = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//随机一个颜色
	imageline($im,rand(0,$width),rand(0,$height),rand(0,$width),rand(0,$height),$c);
 }
 
 //绘制验证码内容(一个一个字符绘制):
 for($i=0;$i<$num;$i++){
	imagettftext($im,18,rand(-40,40),8+(18*$i),24,$color[rand(0,4)],"msyh.ttf",$str[$i]);
 }
//3. 输出图像
header("Content-Type:image/png");//设置响应头信息(注意此函数实行前不可以有输出)
imagepng($im);
//4. 销毁图片(释放内容)
imagedestroy($im);





/**
 * 随机生成一个验证码的内容的函数
 * @param $m :验证码的个数(默认为4)
 * @param $type : 验证码的类型:0:纯数字 1:数字+小写字母  2:数字+大小写字符
 */
function getCode($m=4,$type=0){
	$str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	$t = array(9,35,strlen($str)-1);
	//随机生成验证码所需内容
	$c="";
	for($i=0;$i<$m;$i++){
		$c.=$str[rand(0,$t[$type])];
	}
	return $c;
}

//echo getCode();
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2