Home  >  Article  >  php教程  >  怎样制作PHP验证码? - unique196

怎样制作PHP验证码? - unique196

WBOY
WBOYOriginal
2016-05-20 11:39:161175browse

/**

*制作验证码
*1.启动session
*2.设定标头
*3.创建画布
*4.创建颜色
*5.创建随机数并放到画布上
*6.将得到的若干随机数放入session中
*7.添加干扰点或干扰线
*8.输出画布
*9.销毁画布资源
*/

//1.启动session
session_start();
//2.设定标头指定MIME输出类型
header('Content-Type:image/png');
//3.创建画布
$width = 100;
$height = 30;
$im = imagecreate($width,$height);

//4.创建颜色
$bgcolor = imagecolorallocate($im,255,255,255);
$textcolor = imagecolorallocate($im,0,255,255);
$randcolor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200));

//5.创建随机数并放到画布上
$verify=null;
for($i=0;$i $temp = mt_rand(0,9);
$verify.=$temp;
imagestring($im,5,$i*15+15,8,$temp,imagecolorallocate($im,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200)));
}


//6将生成的随机数放入session中
$_SESSION['verify'] = $verify;

//7.添加干扰点
for($i=0;$i imagesetpixel($im,rand(0,$width),rand(0,$height),imagecolorallocate($im,rand(100,255),rand(100,255),rand(100,255)));
}

//8.将图像输出
imagepng($im); //imagegif()
//9.销毁一图像
imagedestroy($im);

?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn