php图片验证码及其使用

    用于生成图片验证码的文件pic.php,该文件需要一个text参数来取得图片中的验证码!你可以选择验证码的类型!

<?php
header("Content-type: image/jpeg");
$string =  GET['text'];//取得验证码
$width=55;//图片宽度
$height=20;//图片的高度
$noisenum=100;//杂点的个数
$border = true;//边框
$im= imagecreate($width,$height);
$white=imagecolorallocate($im,255,255,255);//图片背景颜色
$red = imagecolorallocate ($im, 255, 0, 0);
$px= (imagesx($im) - 7.5 * strlen($string)) / 2;
if($border==true) imageRectangle($im, 0, 0, $width-1, $height-1, $red);//图片边框
for ($i=0; $i<$noisenum; $i++){
$randColor = imageColorAllocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($im, rand(0, $width), rand(0, $height), $randColor);
}
imagestring($im,10, 5, 2, $string, $red);
imagejpeg($im);
imagedestroy($im);
?>


使用的文件,test.php
<?php
function getrandom ($length =5, $mode = 1){
switch ($mode) {
case '1':
$str = '1234567890';
break;
case '2':
$str = 'abcdefghijklmnopqrstuvwxyz';
break;
case '3':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case '4':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case '5':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
break;
case '6':
$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
break;
default:
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
break;
}

$result = '';
$l = strlen($str);

for($i = 0;$i < $length;$i ++){
$num = rand(0, $l-1);
$result .= $str[$num];
}
return $result;
}

$text=getrandom(2);//取得验证码!
echo "<img a href=/"pic.php?text=$text/">";//显示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值