Session与图片验证码结合使用
<?php
/*
创建随机数-> 写入session ->写入图片
1 imagereatetruecolor新建一个真彩色图像
imagecreatetruecolor(int x_size,int y_size) //x就是宽,y就是高
2 imagecolorallocate为图像分配颜色
imagecolorallocate(resource image,int red,int green,int blue)
3 imagestring绘图函数
imagestring(resource image,font,int x,int y,内容,颜色)
4 dechex()转换成16进制数
5 rand()生成随机数
-->
*/
session_start();//启用session
///************生成4位16进制随机数***********/
for($i=0;$i<4;$i++){
$rand.= dechex(rand(1,15));
}
//写入session
$_SESSION[check]=$rand;
$img=imagecreatetruecolor(100,30);
//设置颜色
$bg = imagecolorallocate($img,0,0,0);//第一次用调试版的时候是背景颜色
$white=imagecolorallocate($img,255,255,255);
//将字符串写在图像上面
imagestring($img,rand(2,6),rand(1,60),rand(1,15),$rand,$white);
//输出图像
header ("Content-type: image/jpeg");
imagejpeg($img);
?>
运行结果: