您的位置:首页 > 理论基础 > 计算机网络

PHP网页中因为 要生成验证码而出现 图像“http://localhost/**.php”因其本身有错无法显示

2012-03-02 22:31 716 查看
我的网页,要在用户登录的位置出现验证码,程序如下:

<?php

header("Content-type:image/jpeg");

ob_end_flush();

$image_width=90;

$image_height=30;

srand(microtime()*100000);

$string="abcdefghigklmnopqrstuvwxyz123456789";

for($i=0;$i<4;$i++){

$new_number.=$string[rand(0,strlen($string)-1)];

}

$_SESSION['check_code_orginal']=$new_number;

$num_image=imagecreate($image_width,$image_height);

$white=imagecolorallocate($num_image,0,0,0);

$gray=imagecolorallocate($num_image,200,200,200);

imagefill($num_image,0,0,$gray);

$li=imagecolorallocate($num_image,220,220,220);

for($i=0;$i<3;$i++){

imageline($num_image,rand(0,30),rand(0,21),rand(20,40),rand(0,21),$li);

}

for($i=0;$i<90;$i++){

imagesetpixel($num_image,rand()*70,rand()*30,$gray);

}

for($i=0;$i<strlen($_SESSION['check_code_orginal']);$i++){

$font=mt_rand(3,5);

$x=mt_rand(1,8)+$image_width*$i/4;

$y=mt_rand(1,$image_height/4);

$color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,155),mt_rand(0,200));

imagestring($num_image,$font,$x,$y,$_SESSION['check_code_orginal'][$i],$color);

}

$picture="image/check_code.jpg";

touch($picture);

imagejpeg($num_image,$picture);

imagedestroy($num_image);

?>

<img src='image/check_code.jpg'>

因为网页上还有其他内容,所以, header("Content-type:image/jpeg"); 位置已经被占用,验证码不能出现了,就会出现图像“http://localhost/**.php”因其本身有错,无法显示” 那么,解决这个问题的最简单的办法就是,直接删除 header("Content-type:image/jpeg");

如果网页上没有其他内容,我们也可以在 header("Content-type:image/jpeg"); 之前加上 ob_clean();

即: ob_clean();

header("Content-type:image/jpeg");

以上是我在程序中碰到的问题,解决方法就是直接删除 header("Content-type:image/jpeg"); 程序便可以运行了。至于原因,不知道我以上的理解的正确否,只是拿来供大家参考。

附一段别人的生成验证码的程序:

<?php

session_start();

//session_register("login_check_number");

//先成生背景,再把生成的验证码放上去

$img_height=70;//先定义图片的长、宽

$img_width=25;

$authnum='';

//生产验证码字符

$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";

$list=explode(",",$ychar);

for($i=0;$i<4;$i++){

$randnum=rand(0,35);

$authnum.=$list[$randnum];

}

//把验证码字符保存到session

$_SESSION["login_check_number"] = $authnum;

$aimg = imagecreate($img_height,$img_width); //生成图片

imagecolorallocate($aimg, 255,255,255); //图片底色,ImageColorAllocate第1次定义颜色PHP就认为是底色了

$black = imagecolorallocate($aimg, 0,0,0); //定义需要的黑色

for ($i=1; $i<=100; $i++) {

imagestring($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),"@",imagecolorallocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));

}

//为了区别于背景,这里的颜色不超过200,上面的不小于200

for ($i=0;$i<strlen($authnum);$i++){

imagestring($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(2,7),mt_rand(1,$img_width/2-2), $authnum[$i],imagecolorallocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));

}

imagerectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//画一个矩形

ob_clean(); //关键代码,防止出现'图像因其本身有错无法显示'的问题。

Header("Content-type: image/PNG");

ImagePNG($aimg);//生成png格式

ImageDestroy($aimg);

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐