开发者社区> 问答> 正文

为什么我的php文件生存的验证码图片在浏览器不显示 :报错

我已经百度了,常用的解决方法都无效。1,GD库我已经打开了。2,关于BOM的那些问题,应该排除了,因为我用Dreamwearve保存文件时是按了取消BOM保存的。3.文件路径是正确的。  究竟是 什么问题,求大神教!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head>

<body>

<div id="center" align="center">

	<p align="center"><strong><font size="+4">用户登录</font></strong></p>
    <form action="/sy20/logindo.php" method="post">
	用户名:<input type="text" name="username" size='21' value=""/>		
    <br />
	密&nbsp;码:  <input type="password" name="password"  size="20"/>
    <br />
   验证码: <input type="text" name="code" size="10"/>
    <img alt="看不清楚,换一张" id="code" src="/sy20/create_code1.php" style="cursor: pointer; vertical-align:middle;" onclick="create_code()"/>
    <button type="button" onClick="create_code()">更换</button>
    </br>
   	<p align="center"><input type="checkbox" name="save" value="1"/> 是否保存用户名&nbsp;&nbsp;&nbsp; &nbsp;<a href="##">忘记密码?</a></p>
	<br />
    
	<input type="submit" value="登录"  name="login"  data-ajax='false'/> 
     <input type="submit" value="注册"  name="zhuce"/>
	</form>

</div> <script> function create_code(){ document.getElementById('code').src='create_code1.php?'+Math.random()*10000; }

</script> </body> </html>



<?php

session_start();
//生成验证码图片
header("Content-type:image/png");
//全数字
$str="1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f";//要显示的字符,自己可以增加或删除
$list=explode(",",$str);
$r=count($list)-1;
$laststr="";
for($i=0;$i<4;$i++){
		$randnum=mt_rand(0,$r);
		$laststr .= $list[$randnum];//取出字符,组合成要显示的字符串
	}
$_SESSION['code']=$laststr;	//将字符串放入SESSION中

$img=imagecreate(28,28);//生成图片
$black=imagecolorallocate($img,0,0,0);	//	设置颜色
$white=imagecolorallocate($img,255,255,255);
$gray=imagecolorallocate($img,200,200,200);
$red=imagecolorallocate($img,255,0,0);
imagefill($img,0,0,white);//给图片填充颜色

//将验证法放入图片
imagestring($im,4,10,8,$laststr,$black);//将验证码放到图片上
for($i=0;$i<50;$i++){//加入干扰元素
imagesetpixel($img,rand()p,rand()0,$black);//加入点状干扰素
imagesetpixel($img,rand()p,rand()0,$red);
imagesetpixel($img,rand()p,rand()0,$gray);
//imagearc($im,rand()p,rand()p,20,20,75,170,$black);//加入弧线干扰素
//imagearc($im,rand()p,rand()p,rand()p,rand()p,$red);//加入弧线干扰素	
	}
	
imagepng($img);
imagedestroy($img);


?>



展开
收起
kun坤 2020-06-06 18:34:41 703 0
1 条回答
写回答
取消 提交回答
  • ######??看不到不知道你回复什么,######

    <?php session_start(); header('Content-type: image/png'); //创建图片 $im = imagecreate($x=130,$y=45 ); $bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色 $fontColor = imageColorAllocate ( $im, 255, 255, 255 );   //字体颜色 $fontstyle = 'rock.ttf';                  //字体样式,这个可以从c:\windows\Fonts\文件夹下找到,我把它放到和authcode.php文件同一个目录,这里可以替换其他的字体样式 //产生随机字符 for($i = 0; $i < 4; $i ++) { $randAsciiNumArray         = array (rand(48,57),rand(65,90)); $randAsciiNum                 = $randAsciiNumArray [rand ( 0, 1 )]; $randStr                         = chr ( $randAsciiNum ); imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr); $authcode                        .= $randStr; } $_SESSION['authcode']        = $randFourStr;//用户和用户输入的验证码做比较 //干扰线 for ($i=0;$i<8;$i++){ $lineColor        = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imageline ($im,rand(0,$x),0,rand(0,$x),$y,$lineColor); } //干扰点 for ($i=0;$i<250;$i++){ imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor); } imagepng($im); imagedestroy($im);
    ?>


    转自: http://www.cnblogs.com/terryglp/articles/1774132.html  

    ######
    for($i=0;$i<50;$i++){//加入干扰元素
    imagesetpixel($img,rand()p,rand()0,$black);//加入点状干扰素
    imagesetpixel($img,rand()p,rand()0,$red);
    imagesetpixel($img,rand()p,rand()0,$gray);
    //imagearc($im,rand()p,rand()p,20,20,75,170,$black);//加入弧线干扰素
    //imagearc($im,rand()p,rand()p,rand()p,rand()p,$red);//加入弧线干扰素  
        }




    这段代码有问题的 rand()p 不能这么写

    ######

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <div id="center" align="center">
            <p align="center"><strong><font size="+4">用户登录</font></strong></p>
            <form action="/sy20/logindo.php" method="post">
            用户名:<input type="text" name="username" size='21' value=""/>       
            <br />
            密 码:  <input type="password" name="password"  size="20"/>
            <br />
           验证码: <input type="text" name="code" size="10"/>
            <img alt="看不清楚,换一张" id="code" src="png1.php" style="cursor: pointer; vertical-align:middle;" onclick="create_code()"/>
            <button type="button" onClick="create_code()">更换</button>
            </br>
            <p align="center"><input type="checkbox" name="save" value="1"/> 是否保存用户名     <a href="##">忘记密码?</a></p>
            <br />
             
            <input type="submit" value="登录"  name="login"  data-ajax='false'/> 
             <input type="submit" value="注册"  name="zhuce"/>
            </form>
         
    </div>
    <script>
    function create_code(){
        document.getElementById('code').src='png1.php?'+Math.random()*10000;    
        }
    </script>
    </body>
    </html>



    ######

    引用来自“氪金”的评论

    <?php
    session_start();
    header('Content-type: image/png');
    //创建图片
    $im = imagecreate($x=130,$y=45 );
    $bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色
    $fontColor = imageColorAllocate ( $im, 255, 255, 255 );   //字体颜色
    $fontstyle = 'rock.ttf';                  //字体样式,这个可以从c:\windows\Fonts\文件夹下找到,我把它放到和authcode.php文件同一个目录,这里可以替换其他的字体样式
    //产生随机字符
    for($i = 0; $i < 4; $i ++) {
            $randAsciiNumArray         = array (rand(48,57),rand(65,90));
            $randAsciiNum                 = $randAsciiNumArray [rand ( 0, 1 )];
            $randStr                         = chr ( $randAsciiNum );
            imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
            $authcode                        .= $randStr; 
    }
    $_SESSION['authcode']        = $randFourStr;//用户和用户输入的验证码做比较
    //干扰线
    for ($i=0;$i<8;$i++){
            $lineColor        = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
            imageline ($im,rand(0,$x),0,rand(0,$x),$y,$lineColor);
    }
    //干扰点
    for ($i=0;$i<250;$i++){
            imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor);
    }
    imagepng($im);
    imagedestroy($im);                
    ?>


    转自: http://www.cnblogs.com/terryglp/articles/1774132.html  

    我把你给的这段代码放上去,还是没有显示 ######首先确认,你直接运行 png.php (暨我帖的生成图片的那段程序),是出什么结果,弄不好你没有开gd库。######ob_clean()
    2020-06-06 18:34:54
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
PHP安全开发:从白帽角度做安全 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载