<?php session_start(); function GetVerify($length) { $strings = Array('3','4','5','6','7','a','b','c','d','e','f','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y'); $chrNum = ""; $count = count($strings); for ($i = 1; $i <= $length; $i++) { //循环随机取字符生成字符串$chrNum .= $strings[rand(0,$count-1)]; } return $chrNum; } $fontSize = 15; //定义字体大小 $length = 4; //定义字符串长度 $strNum = GetVerify($length); //获取一个随机字符串 $_SESSION['verify'] = $strNum; //付值给session $width = 70; //定义图片宽度 $height = 24; //定义图片高度 $im = imagecreate($width,$height); //生成一张指定宽高的图片 $backgroundcolor = imagecolorallocate ($im, 255, 255, 255); //生成背景色 $frameColor = imageColorAllocate($im, 150, 150, 150); //生成边框色 $font = realpath("arial.ttf"); //提取字体文件,开始写字 for($i = 0; $i < $length; $i++) { $charY = ($height+9)/2 + rand(-1,1); //定义字符Y坐标 $charX = $i*15+8; //定义字符X坐标 //生成字符颜色 $text_color = imagecolorallocate($im, mt_rand(50, 200), mt_rand(50, 128), mt_rand(50, 200)); $angle = rand(-20,20); //生成字符角度 //写入字符 imageTTFText($im, $fontSize, $angle, $charX, $charY, $text_color, $font, $strNum[$i]); } for($i=0; $i <= 5; $i++) { //循环画背景线 $linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); $linex = mt_rand(1, $width-1); $liney = mt_rand(1, $height-1); imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor); } for($i=0; $i <= 32; $i++) { //循环画背景点,生成麻点效果 $pointcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($im, mt_rand(1, $width-1), mt_rand(1, $height-1), $pointcolor); } imagerectangle($im, 0, 0, $width-1 , $height-1 , $frameColor); //画边框 ob_clean(); header('Content-type: image/png'); imagepng($im); imagedestroy($im); /** * 功能:生成随机字符串 */ exit; ?>
2.验证码赋值给$_SESSION['lgcode']
<?php //获取随机字符 $rndstring = ""; for($i=0;$i<4;$i++){ $rndstring .= chr(mt_rand(65,90)); } //如果支持GD,则绘图 if(function_exists("imagecreate")) { //PutCookie("dd_ckstr",strtolower($rndstring),1800,"/"); session_register('lgcode'); $_SESSION['lgcode'] = strtolower($rndstring); $rndcodelen = strlen($rndstring); //图片大小 $im = imagecreate(50,20); //字体 $font_type = dirname(__FILE__)."/./ant2.ttf"; //背景颜色 $bgcolor = ImageColorAllocate($im, 245,245,245); //边框色 $iborder = ImageColorAllocate($im, 0x71,0x76,0x67); //字体色 //不支持 imagettftext $fontColor = ImageColorAllocate($im, 0x50,0x4d,0x47); //支持 imagettftext $fontColor2 = ImageColorAllocate($im, 0x36,0x38,0x32); //阴影 $fontColor1 = ImageColorAllocate($im, 0xbd,0xc0,0xb8); //杂点背景线 //$lineColor1 = ImageColorAllocate($im, 130,220,245); //$lineColor2 = ImageColorAllocate($im, 225,245,255); //背景线 //for($j=3;$j<=16;$j=$j+3) imageline($im,2,$j,48,$j,$lineColor1); //for($j=2;$j<52;$j=$j+(mt_rand(3,6))) imageline($im,$j,2,$j-6,18,$lineColor2); //边框 imagerectangle($im, 0, 0, 49, 19, $iborder); $strposs = array(); //文字 for($i=0;$i<$rndcodelen;$i++){ if(function_exists("imagettftext")){ $strposs[$i][0] = $i*10+6; $strposs[$i][1] = mt_rand(15,18); imagettftext($im, 11, 5, $strposs[$i][0]+1, $strposs[$i][1]+1, $fontColor1, $font_type, $rndstring[$i]); } else{ imagestring($im, 5, $i*10+6, mt_rand(2,4), $rndstring[$i], $fontColor); } } //文字 for($i=0;$i<$rndcodelen;$i++){ if(function_exists("imagettftext")){ imagettftext($im, 11,5, $strposs[$i][0]-1, $strposs[$i][1]-1, $fontColor2, $font_type, $rndstring[$i]); } } header("Pragma:no-cache\r\n"); header("Cache-Control:no-cache\r\n"); header("Expires:0\r\n"); //输出特定类型的图片格式,优先级为 gif -> jpg ->png if(function_exists("imagejpeg")){ header("content-type:image/jpeg\r\n"); imagejpeg($im); }else{ header("content-type:image/png\r\n"); imagepng($im); } ImageDestroy($im); }else{ //不支持GD,只输出字母 ABCD //PutCookie("dd_ckstr","abcd",1800,"/"); session_register('lgcode'); $_SESSION['lgcode'] = "abcd"; header("content-type:image/jpeg\r\n"); header("Pragma:no-cache\r\n"); header("Cache-Control:no-cache\r\n"); header("Expires:0\r\n"); $fp = fopen("./vdcode.jpg","r"); echo fread($fp,filesize("./vdcode.jpg")); fclose($fp); } ?>
3.需要根据给定点问题给出答案的,适用于留言
<?php session_start(); function getCode ($length = 32, $mode = 0) { switch ($mode) { case '1': $str = '123456789'; break; case '2': $str = 'abcdefghijklmnopqrstuvwxyz'; break; case '3': $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case '4': $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break; case '5': $str = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789'; break; case '6': $str = 'abcdefghijklmnopqrstuvwxyz1234567890'; break; default: $str = 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'; break; } $result = ''; $l = strlen($str)-1; for($i = 0;$i < $length;$i ++){ $num = rand(0, $l); $result .= $str[$num]; } return $result; } //建立验证图片 function createAuthNumImg($randStr,$imgW=100,$imgH=40,$fontName) { header ("content-type: image/png"); $image = imagecreate($imgW , $imgH); $color_white = imagecolorallocate($image , 255 , 255 , 255); $color_gray = imagecolorallocate($image , 228 , 228 , 228); $color_black = imagecolorallocate($image , 255 , 102 , 204); for ($i = 0 ; $i < 1000 ; $i++) { imagesetpixel($image , mt_rand(0 , $imgW) , mt_rand(0 , $imgH) , $color_gray); } imagerectangle($image , 0 , 0 , $imgW - 1 , $imgH - 1 , $color_gray); for ($i=10;$i<$imgH;$i+=10)imageline($image, 0, $i, $imgW, $i, $color_gray); imagettftext($image,16,5,3,25,$color_black,$fontName,$randStr); for ($i=10;$i<$imgW;$i+=10)imageline($image, $i, 0, $i, $imgH, $color_gray); imagepng($image); imagedestroy($image); } $a=GetCode(1,1); $b=GetCode(1,1); $c=GetCode(1,1); $Passport=$a."+".$b."+".$c; $Total=$a+$b+$c; $_SESSION[$_GET['Action']]=md5(strtoupper($Total)); createAuthNumImg($Passport,$_GET['imgW'],$_GET['imgH'],"./verdana.ttf"); ?>