咨询电话:186 7916 6165 咨询电话:186 7916 6165 (微信同号)    在线QQ:181796286
NEWS BLOG ·
学无止境
关注开优网络 关注前沿
PHP Web开发之操作XML
PHP自定义分页类

PHP自定义验证码

发表日期:2016-01-26    文章编辑:南昌开优网络    浏览次数:4436    标签:PHP应用

php验证码
<?php
session_start();
$rnd_code=4;
for($i=0;$i<$rnd_code;$i++){
$nmsg .=dechex(mt_rand(0,15));
}
$_SESSION["code"]=$nmsg;
$width=75;
$height=25;
$img=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($img,255,255,255);
imagefill($img,0,0,$white);
//边框
$flag=false;
if($flag){
$black=imagecolorallocate($img,0,0,0);
imagerectangle($img,0,0,$width-1,$height-1,$black);
}

//随机线条
for($i=0;$i<6;$i++){
$rnd_color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$rnd_color);
}
//雪花
for($i=0;$i<100;$i++){
$rnd_color=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($img,1,mt_rand(1,$width),mt_rand(1,$height),'*',$rnd_color);
}
//生成随机验证码
for($i=0;$i<strlen($_SESSION['code']);$i++){
$rnd_color=imagecolorallocate($img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($img,5,$i*$width/$rnd_code+mt_rand(1,10),mt_rand(1,$height/2),$_SESSION['code'][$i],$rnd_color);
}
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>