Heim  >  Artikel  >  Backend-Entwicklung  >  怎样制作PHP验证码?

怎样制作PHP验证码?

WBOY
WBOYOriginal
2016-06-23 13:08:18784Durchsuche

/**

*制作验证码
*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);

?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn