• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php用header('content-type: image/png')输出验证码,但响应回来的是text/html

php 搞代码 4年前 (2022-01-25) 13次浏览 已收录 0个评论
文章目录[隐藏]

<body>

<code><?phpheader("Content-type: image/png");session_start();$im<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i><strong>搞代gaodaima码</strong>age = imagecreatetruecolor(70,30);$bgcolor = imagecolorallocate($image,255,255,255);imagefill($image,0,0,$bgcolor);// 生成干扰像素点for ($i=0; $i < 200; $i++) {    $pixelcolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));    imagesetpixel($image,rand(1,69),rand(1,29),$pixelcolor);}    // 生成干扰直线for ($i=0; $i < 4; $i++) {    $linecolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));    imageline($image,rand(0,69),rand(0,29),rand(0,69),rand(0,29),$linecolor);}$captch_code = "";// 生成验证码for ($i=0; $i < 4; $i++) {    $fontsize = 6;    $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));    $data = 'abcdefghijkmnpqrstuvwxy3456789';    $fontcontent = substr($data,rand(0,strlen($data)-1),1);    $captch_code .= $fontcontent;    $x = ($i*70/4)+rand(5,10);    $y = rand(5,10);    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);}$_SESSION['authcode'] = $captch_code;imagepng($image);imagedestroy($image);</code>

上面为生成验证码的代码,在本地服务器可以生成验证码,但把代码上传到远程服务器后就无法生成验证码,返回500(intetnal server error)错误,远程服务器已开启GD库,代码开头header(‘content-type: image/png’),但响应回来的却是text/html。

远程服务器返回text/html

本地成功返回image/png

回复内容:

<body>

<code><?phpheader("Content-type: image/png");session_start();$image = imagecreatetruecolor(70,30);$bgcolor = imagecolorallocate($image,255,255,255);imagefill($image,0,0,$bgcolor);// 生成干扰像素点for ($i=0; $i < 200; $i++) {    $pixelcolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));    imagesetpixel($image,rand(1,69),rand(1,29),$pixelcolor);}    // 生成干扰直线for ($i=0; $i < 4; $i++) {    $linecolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));    imageline($image,rand(0,69),rand(0,29),rand(0,69),rand(0,29),$linecolor);}$captch_code = "";// 生成验证码for ($i=0; $i < 4; $i++) {    $fontsize = 6;    $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));    $data = 'abcdefghijkmnpqrstuvwxy3456789';    $fontcontent = substr($data,rand(0,strlen($data)-1),1);    $captch_code .= $fontcontent;    $x = ($i*70/4)+rand(5,10);    $y = rand(5,10);    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);}$_SESSION['authcode'] = $captch_code;imagepng($image);imagedestroy($image);</code>

上面为生成验证码的代码,在本地服务器可以生成验证码,但把代码上传到远程服务器后就无法生成验证码,返回500(intetnal server error)错误,远程服务器已开启GD库,代码开头header(‘content-type: image/png’),但响应回来的却是text/html。

远程服务器返回text/html

本地成功返回image/png

都500错误了你还想啥呢?

控制台点那个500看具体错误响应内容,如果没有开启错误打印

1- 远端服务器没有开启php的错误输出,此时当致命错误发生的时候直接返回web server 500错误,需要开启错误输出或查看php的错误日志获知具体错误。
2- 不应该在代码的开始处使用header(‘image/png’);而是应该将该行移到imagepng($image);之前,否则就算有错误信息也会由于错误的头导致无法看到正确的结果。

感觉是500错误后,没有调用到header(“Content-type: image/png”);
因为500错误本身就要返回的是一些错误文本,因此就成text/html了

出错了,没有执行到header png这一块的逻辑


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php用header('content-type: image/png')输出验证码,但响应回来的是text/html

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址