php制作验证码
利用画布制作验证码
? function Vchar(){ ???????????//创建画布 ???????????$image=imagecreatetruecolor(100,30);
//为画布填充颜色 ???????????$bg=imagecolorallocate($image,mt_rand(210,255),mt_rand(210,255),mt_rand(210,255)); ???????????imagefill($image,0,0,$bg); ???????????//将验证码绘制到画布 ???????????$str=getcode();//获得4个随机的字母数字组合 ???????????for($i=0;$i<4;$i++){//把这四个字母绘制到画布上 ???????????????$color=imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); ???????????????imagettftext($image,24,mt_rand(-40,40),18*($i+1),24,$color,realpath(FRAME_ROOT."/Common/simhei.ttf"),$str[$i]); ???????????} ???????????//绘制线条遮挡文字 ???????????for($j=0;$j<4;$j++){ ???????????????$color2=imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); ???????????????imageline($image,mt_rand(0,100),mt_rand(0,30),mt_rand(0,100),mt_rand(0,30),$color2); ???????????} ???????????header("Content-type:image/jpeg"); ???????????imagejpeg($image); ???????????} ???????//随机获取六位数字 function getcode(){ ???????????????$code="abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVEXYZ23456789"; ???????????????$str=""; ???????????????for($i=0;$i<4;$i++){ ???????????????$str.=$code[mt_rand(0,57)]; ???????????????} ???????????????return $str; ???????????}
Vchar();//调用绘制验证码函数 ??????//具体的利用方案:上面那段代码是php代码名字叫做validate.php;暂且假设下面这个a.html文件和这个validate.php在同一目录下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<img src="验证码.php" ?onclick="this.src=‘validate.php?‘+Math.random()" >//之所以要加这个Math.random()这个随机函数是为了每次点击是更换验证码。
//最后的效果:
</body>
</html>
php制作验证码
原文地址:https://www.cnblogs.com/xhen/p/9991754.html