PHP图片验证码

来源:互联网 发布:泰格软件官网 编辑:程序博客网 时间:2024/04/29 04:54

PHP图片验证码原代码,需支持gd2.dll扩展,需修改PHP.ini后重启IIS or apache

调用方法<img src="this.php">

------------------------------------------------------

程序代码:

<?PHP
session_start();
session_register('SafeCode');
$type = 'gif';
$width= 40;
$height= 16;
header("Content-type: image/".$type);
srand((double)microtime()*1000000);
$randval = randStr(4,"");
if($type!='gif' && function_exists('imagecreatetruecolor')){
$im = @imagecreatetruecolor($width,$height);
}else{
$im = @imagecreate($width,$height);
}
$r = Array(225,211,255,223);
$g = Array(225,236,237,215);
$b = Array(225,236,166,125);

$key = rand(0,3);

$backColor = ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)
$borderColor = ImageColorAllocate($im, 0, 0, 0);//边框色
$pointColor = ImageColorAllocate($im, 255, 170, 255);//点颜色

@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置
@imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置
$stringColor = ImageColorAllocate($im, 255,51,153);

for($i=0;$i<=100;$i++){
$pointX = rand(2,$width-2);
$pointY = rand(2,$height-2);
@imagesetpixel($im, $pointX, $pointY, $pointColor);
}

@imagestring($im, 3, 5, 1, $randval, $stringColor);
$ImageFun='Image'.$type;
$ImageFun($im);
@ImageDestroy($im);
$_SESSION['SafeCode'] = $randval;
//产生随机字符串
function randStr($len=6,$format='ALL') {
switch($format) {
case 'ALL':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
case 'CHAR':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
case 'NUMBER':
$chars='0123456789'; break;
default :
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
}
$string="";
while(strlen($string)<$len)
$string.=substr($chars,(mt_rand()%strlen($chars)),1);
return $string;
}
?>

-----------------------------

<?php
// BY PHP 国度 www.phphot.be
//关闭报错
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;$i<strlen($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;
}
?> 

*********************************************************

 
原创粉丝点击