php生成带干扰的验证码程序
这里介绍了一款安全性比较高的验证生成程序,可以带干扰线等内容,可以有效的防止用户用程序识别验证码的难度了,代码如下:
- <?php
-
-
-
-
-
- session_start();
- function RandAscii($number){
- $arr=array('0','1','2','3','4','5','6','7','8','9',
- 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','w','v','u','x','y','z');
- for ($i=1;$i<=$number;$i++)
- {
- $rand= $rand.$arr[rand(0,35)];
- }
- return $rand;
- }
- $rand=RandAscii(4);
-
- $_SESSION['check_pic']=$rand;
- $x=80;
- $y=24;
- $im=imagecreatetruecolor($x,$y);
- $bg=imagecolorallocate($im,255,255,255);
- imagefill( $im,0,0,$bg);
- $wh=imagecolorallocate($im,255,255,0);
- $grey=imagecolorallocate($im,128,128,128);
- $yellow=imagecolorallocate($im,255,255,0);
- $red=imagecolorallocate($im,0,255,0);
- $foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
- imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
- imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
- imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
- );
-
- $border = imagecolorallocate($im, 133, 153, 193);
- imagerectangle($im, 0, 0, $x - 1, $y - 1, $border);
-
- for($i=0;$i<10;$i++){
- imageline($im,rand(0,60),2,rand(0,60),20,$yellow);
-
- }
- for($j=0;$j<100;$j++){
- imagesetpixel($im,rand()%76,rand()%20,$red);
- }
-
- imagettftext($im, 14,rand(30, -30), 5, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[0]);
- imagettftext($im, 14,rand(50, -50), 24, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[1]);
- imagettftext($im, 14,rand(50, -50), 43, rand(15, 18) ,$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[2]);
- imagettftext($im, 14,rand(30, -30), 62, rand(15, 18),$foregroundArr[rand(0,3)], 'C:WindowsFontsArial.ttf',$rand[3]);
-
- header("Content-type: image/jpeg");
- imagejpeg($im);
- imagedestroy($im);
- ?>
调用方法,代码如下:
- <?php
-
-
-
-
-
- session_start();
- if(isset($_POST['check']))
- {
- if($_POST['check'])
- {
- if($_POST['check']==$_SESSION['check_pic'])
- {
- echo " 验证码正确".$_SESSION['check_pic'];
- }
- else
- {
- echo " 验证码错误".$_SESSION['check_pic'];
- }
- }
- }
- ?>
- <FORM METHOD=POST ACTION="">
- <img src="index.php"><br> <!----链接图片--->
- <input type="text" name="check" >
- <input type="submit" value="提交">
- </FORM>
|