上一篇文章.讲到建立一个表单给自己发送邮件.如果碰到Spam就麻烦了.所以要验证一下比较好.今天介绍一个用图片验证的办法.
写个生成图片的的php文件.我们命名为yz_img.php,以下是代码.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php session_start(); //生成验证码图片 Header("Content-type: image/PNG"); $im = imagecreate(44,15); $back = ImageColorAllocate($im, 245,245,245); imagefill($im,0,0,$back); //背景 srand((double)microtime()*1000000); //生成4位数字 for($i=0;$i<4;$i++){ $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); $authnum=rand(1,9); $yzm.=$authnum; imagestring($im, 5, 2+$i*10, 1, $authnum, $font); } for($i=0;$i<100;$i++) //加入干扰象素 { $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); } ImagePNG($im); ImageDestroy($im); $_SESSION['yzm'] = $yzm;//给session赋值 ?> |
在要插入图片的地方插入这个图片.
即:
<img src="yz_img.php" alt="验证码" />最后验证一下$_SESSION['yzm']验证搞定.
声明:本站遵循署名-非商业性使用-相同方式共享3.0共享协议. 转载请注明转自 PhilNa ™
呃,話說又被我占著沙發了?最近也在學php了我~