php实现简单验证码

验证码实现

1.根据提供字母或数字随机生成四位数验证码

2.将验证码存入session中

3.将验证码放入图片中

	//根据提供字符生成四位验证码
		$str = 'a,b,c,d,e,f,g,h,m,n,p,q,l,r,t,2,3,4,5,6,7,8,9,';
		$list = explode(',', $str);
		$max = count($list)-1;
		$verifyCode = '';
		for ($i=0; $i < 4; $i++) { 
			$randnum = mt_rand(0,$max);
			$verifyCode .= $list[$randnum];
		}
		//存进session
		$_SESSION['code'] = $verifyCode;
		//设置变量从0到70和从0到30的随机数	
		$rand_x = rand(0,70);
		$rand_y = rand(0,30);
		$color = rand(0,200);
		//创建画布
		$img = imagecreatetruecolor(70, 30);
		
		$blue = imagecolorallocate($img, 100, 190, 100);
		$block = imagecolorallocate($img, 255, 255, 255);
		//干扰元素颜色
		$red = imagecolorallocate($img, 255, 0, 0);
		$blue = imagecolorallocate($img, 0, 0, 255);
		$green = imagecolorallocate($img, 0, 255, 0);		
		//干扰元素
		for($i=1;$i<50;$i++){
			imagesetpixel($img, rand(0,70), rand(0,30), $blue);
			imagesetpixel($img, rand(0,70), rand(0,30), $red);
			imagesetpixel($img, rand(0,70), rand(0,30), $green);
		}
		imagearc($img, $rand_x, $rand_y, $rand_x, $rand_y, $rand_x, $rand_y, $red);
		imageline($img, $rand_x, $rand_y, $rand_x, $rand_y, $green);
		//设置背景填充色
		imagefill($img, 0, 0, $blue);
		//把验证码放进画布
		imagestring($img, 18, 10, 10, $verifyCode, $block);
		header('Content-Type:image/png');
		//设置图片格式
		imagepng($img);
		imagedestroy($img);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值