澳门新葡萄京官网首页php入门级的PHP验证码程序

需要gd库支持.Image 图像函数参见 BY PHP 国度
关闭报错error_reporting(0);//图片宽度$x_size=60;//图片高度$y_size=20;//生成4个随机字符$nmsg=num_rand(4);//支持安全通连接吗?$S=$_SERVER[‘SERVER_PORT’]==’443′
? 1:0;//使用cookie记录随机码//expire=0 cookie将会在会话结束后失效。
setCookie(‘ck_num’,md5($nmsg),0,’/’,”,$S);//创建宽:60*20的图片(画布)$aimg
= imagecreate($x_size,$y_size);//设置图片背景色$back =
imagecolorallocate($aimg, 255, 255, 255);//设置字体颜色$border =
imagecolorallocate($aimg, 0, 0,
0);//从0,0点填充59*19的白色矩形区域imagefilledrectangle($aimg, 0, 0,
$x_size – 1, $y_size – 1,
$back);//从0,0点绘制59*19的黑色矩形边框imagerectangle($aimg, 0, 0,
$x_size – 1, $y_size – 1, $border);for ($i=0;$istrlen($nmsg);$i++){
//在图片上写字 imageString($aimg,5,$i*$x_size/4+3,2,
$nmsg[$i],$border); }header(“Content-type:
image/png”);imagepng($aimg);imagedestroy($aimg);exit;function
num_rand($lenth){ // 播下一个随机数发生器种子
//php自4.2.0起,,此参数变为可选项,当该项为空时,会被设为随时数
mt_srand((double)microtime() * 1000000); //产生有4个随机数字的字符串
for($i=0;$i$lenth;$i++){ $randval.= mt_rand(0,9); }
//对含有4个数字的字符串使用md5加密,长度是32位的
//从3长度为32的字符中,自最小数起或最大数32-$lenth起,取长度为$lenth的字符串
$randval=substr(md5($randval),mt_rand(0,32-$lenth),$lenth); return
$randval;}?

实例

session_start();

 代码如下

$enablegd = 1;
$funcs =
array(‘imagecreatetruecolor’,’imagecolorallocate’,’imagefill’,’imagestring’,’imageline’,’imagerotate’,’imagedestroy’,’imagecolorallocatealpha’,’imageellipse’,’imagepng’);
foreach($funcs as $func)
{
 if(!function_exists($func))
 {
  $enablegd = 0;
  break;
澳门新葡萄京官网首页, }
}

<?php 
session_start(); 
 
$im = imagecreate(80,30);//创建图片 
$color =
imagecolorallocate($im,rand(150,200),rand(150,200),rand(150,200));//设置图片背景 
$str =
“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789”;//产生随机字符串 
 for($i=0;$i<5;$i++){ 
        $code .= $str[rand(0,(strlen($str)-1))]; 
        } 
$_SESSION[‘code’] = $code; 
for($a=0;$a<5;$a++){  //将字符串写入图片资源 
    $x = $a*10 + 15; 
    $y = rand(5,10);  // www.111cn.net
    imagechar($im,5,$x,$y,$code{$a},imagecolorallocate($im,0,0,0)); 
    } 
header(“Content-type:image/png”);//输出图片资源 
imagepng($im); 
?>

ob_clean();

例2

if($enablegd)
{
 //create captcha
 $consts = ‘cdfgkmnpqrstwxyz23456’;
 $vowels = ‘aek23456789’;
 for ($x = 0; $x < 6; $x++)
 {
  $const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1);
  $vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1);
 }
 $radomstring = $const[0] . $vow[0] .$const[2] . $const[1] .
$vow[1] . $const[3] . $vow[3] . $const[4];
 $_SESSION[‘checkcode’] = $string = substr($radomstring,0,4); //only
display 4 str
 //set up image, the first number is the width and the second is the
height
 $imageX = strlen($radomstring)*8; //the image width
 $imageY = 20;      //the image height
 $im = imagecreatetruecolor($imageX,$imageY);

 代码如下

 //creates two variables to store color
 $background = imagecolorallocate($im, rand(180, 250), rand(180, 250),
rand(180, 250));
 $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)));
 $foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20,
100), rand(20, 100),80);
 $middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160),
rand(200, 160));
 $middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180,
140), rand(180, 140),80);

<?php
if(!isset($_SESSION)){ //判断session是否开启
session_start(); //开启就session
}
$width=70; //布画宽度
$height=25; //布画高度
$length=4;//验证码长度
$code=getcode($length); //获取随机字符串
$_SESSION[‘verfyCode’] = $code;

 //fill image with bgcolor
 imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
 //writes string
 imagettftext($im, 12, rand(30, -30), 5, rand(14, 16),
$foregroundArr[rand(0,3)], PHPCMS_ROOT.’include/fonts/ALGER.TTF’,
$string[0]);
 imagettftext($im, 12, rand(50, -50), 20, rand(14, 16),
$foregroundArr[rand(0,3)], PHPCMS_ROOT.’include/fonts/ARIALNI.TTF’,
$string[1]);
 imagettftext($im, 12, rand(50, -50), 35, rand(14, 16),
$foregroundArr[rand(0,3)], PHPCMS_ROOT.’include/fonts/ALGER.TTF’,
$string[2]);
 imagettftext($im, 12, rand(30, -30), 50, rand(14, 16),
$foregroundArr[rand(0,3)], PHPCMS_ROOT.’include/fonts/arial.ttf’,
$string[3]);
 
 //strikethrough

$img=imagecreate($width,$height);
$bgcolor=imagecolorallocate($img,240,240,240);
$rectangelcolor=imagecolorallocate($img,150,150,150);
imagerectangle($img,1,1,$width-1,$height-1,$rectangelcolor);//画边框
for($i=0;$i<$length;$i++){//循环写字
$codecolor=imagecolorallocate($img,mt_rand(50,200),mt_rand(50,128),mt_rand(50,200));
$angle=rand(-20,20);
$charx=$i*15+8;
$chary=($height+14)/2+rand(-1,1);
imagettftext($img,15,$angle,$charx,$chary,$codecolor,’C:WINDOWSFontsSIMKAI.TTF’,
$code[$i]);
}
for($i=0;$i<20;$i++){//循环画线
$linecolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
$linex=mt_rand(1,$width-1);
$liney=mt_rand(1,$height-1);
imageline($img,$linex,$liney,$linex+mt_rand(0,4)-2,$liney+mt_rand(0,4)-2,$linecolor);
}
for($i=0;$i<100;$i++){//循环画点
$pointcolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
imagesetpixel($img,mt_rand(1,$width-1),mt_rand(1,$height-1),$pointcolor);
}
function
getcode($length){//生成php随机数
$pattern =
‘1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
‘;//字符池
for($i=0;$i<$length;$i++) {
$key .= $pattern{mt_rand(0,35)};
}
return $key;

 $border = imagecolorallocate($im, 133, 153, 193);
 //imagefilledrectangle($aimg, 0, 0, $x_size – 1, $y_size – 1,
$back);
 imagerectangle($im, 0, 0, $imageX – 1, $imageY – 1, $border);

}
ob_clean();
header(‘Content-type:image/png’);
imagepng($img);
?>

 $pointcol = imagecolorallocate($im, rand(0,255), rand(0,255),
rand(0,255));
 for ($i=0;$i<80;$i++)
 {
  imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
 }
 //random shapes
 for ($x=0; $x<9;$x++)
 {
  if(mt_rand(0,$x)%2==0)
  {
   imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0,
120), rand(0, 999999));
   imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0,
120), $middleground2);
  }
  else
  {
   imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0,
120), rand(0, 999999));
   imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0,
120), $middleground);
  }
 }
 //output to browser
    header(“content-type:image/pngrn”);
 imagepng($im);
 imagedestroy($im);
}
else
{
 $files = glob(PHPCMS_ROOT.’images/checkcode/*.jpg’);
 if(!is_array($files))
exit($LANG[‘please_check_dir_images_checkcode’]);

效果图如下

 $checkcodefile = $files[rand(0, count($files)-1)];
 $_SESSION[‘checkcode’] = substr(basename($checkcodefile), 0, 4);

澳门新葡萄京官网首页 1

 header(“content-type:image/jpegrn”);
 include $checkcodefile;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注