Rumah >pembangunan bahagian belakang >tutorial php >php?生成验证码解决思路
php?生成验证码
哪位大哥分享下代码
------解决思路----------------------
session_start();<br />$dict = str_split('abcdefghijklmnopqrstuv0123456789');<br />shuffle($dict);<br />$_SESSION['word'] = join('', array_slice($dict, -4));<br /><br />$im = imagecreate(100, 30);<br />$bg = imagecolorallocate($im, 255, 255, 255);<br />$char = imagecolorallocate($im, 0, 0, 0);<br />imagestring($im, 5, 10, 10, $_SESSION['word'], $char);<br />imagegif($im);
<br /> session_start();<br /> $checkCode="";<br /> for($i=0;$i<4;$i++){<br /> $checkCode .=substr('abcdefghijklmnopqrstuvwxyz0123456789', rand(0,35), 1); // $checkCode.=dechex(rand(1,15));<br /> }<br /> //讲随机验证码保存到session中<br /> $_SESSION['myCheckCode']=$checkCode;<br /> //创建图片,并把随机数画上去<br /> $img=imagecreatetruecolor(100,30);<br /> //背景默认就是黑色<br /> //你可以指定背景颜色<br /> $bgcolor=imagecolorallocate($img,0,0,0);<br /> imagefill($img,0,0,$bgcolor);<br /> //创建新的颜色<br /> $white=imagecolorallocate($img,255,255,255);<br /> $blue=imagecolorallocate($img,0,0,255);<br /> $red=imagecolorallocate($img,255,0,0);<br /> $green=imagecolorallocate($img,255,0,0);<br /><br /> //画出干扰线段<br /> for($i=0;$i<20;$i++){<br /> //更好的方法是颜色随机<br /> imageline($img,rand(0,110),rand(0,30),rand(0,110),rand(0,30),imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255)));<br /> <br /> }<br /> //画出噪点,自己画.<br /> <br /> //把四个随机值画上去 <br /> imagestring($img,rand(1,5),rand(2,80),rand(2,10),$checkCode,$white);<br /> <br /> //如果要使用中文<br /> //array imagefttext ( string $font_file , string $text [, array $extrainfo ] )<br /> //imagettftext($img,15,10,20,25,$white,"STXINWEI.TTF","北京你好");<br /> //输出<br /> header("content-type: image/png");<br /> imagepng($img);<br /><br />