include '../../lib/functions.php';if(!isset($SESSION))
{
session_start();
}$r=null;
for($i=0;$i<4;$i++)
{
$r.=dechex(rand(1,15));
}
$SESSION['check_pic']=$r;//随机数存储在session中
$img=imagecreatetruecolor(100,40)or die("建立图片失败");
$bg=imagecolorallocate($img,0,0,0);//第一次使用创建的是背景颜色$cl=imagecolorallocate($img,rand(1,250),rand(1,250),rand(1,254));//$img ,red,blue,grenn
imagestring($img,5,rand(0,40),rand(0,20),$r,$cl);//$img,font,x,y,string,colorheader("content-type:image/jpeg");//页面输出为一个图片格式
imagejpeg($img);// 以 filename 为文件名创建一个 JPEG 图像。
imagedestroy($img);验证码输出代码:为啥一加第一行include代码,图片就不显示了。functions.php显示一组随机字符串。
header("content-type:image/jpeg")前难道不能加include语句?
{
session_start();
}$r=null;
for($i=0;$i<4;$i++)
{
$r.=dechex(rand(1,15));
}
$SESSION['check_pic']=$r;//随机数存储在session中
$img=imagecreatetruecolor(100,40)or die("建立图片失败");
$bg=imagecolorallocate($img,0,0,0);//第一次使用创建的是背景颜色$cl=imagecolorallocate($img,rand(1,250),rand(1,250),rand(1,254));//$img ,red,blue,grenn
imagestring($img,5,rand(0,40),rand(0,20),$r,$cl);//$img,font,x,y,string,colorheader("content-type:image/jpeg");//页面输出为一个图片格式
imagejpeg($img);// 以 filename 为文件名创建一个 JPEG 图像。
imagedestroy($img);验证码输出代码:为啥一加第一行include代码,图片就不显示了。functions.php显示一组随机字符串。
header("content-type:image/jpeg")前难道不能加include语句?
这个是functions.php的代码:
function random_text($count,$rm_similar=false)//$count 取的位数,$rm_similar 是否调用unset函数
{
$chars= array_flip(array_merge(range(0,9),range('a','z')));
//range 创建并返回一个包含指定范围的元素的数组,array_flip反转数组,array_merge()把两个数组合并为一个数组
if($rm_similar)
{
unset($chars[0],$chars[1],$chars[2],$chars[5],$chars[8],$chars['b'],$chars['i'],$chars['o'],$chars['q'],
$chars['s'],$chars['u'],$chars['v'],$chars['z']);
//unset 删除数组中指定的数据
}
$text='';
for($i=0;$i<$count;$i++)
{
$text.=array_rand($chars);//从数组中随机选出一个或多个元素,并返回
}
return $text;
}