无法生成验证码图像,已输出内容

时间:2012-05-23 15:34:37

标签: php apache google-chrome captcha

我使用secureimage解决方案生成验证码

我在chrom控制台上有这个错误

  

资源解释为图像但使用MIME类型text / html传输

并且当这个url直接用作图像src

  

securimage_show.php?SID = fa5e1eb19c3a534885632e

我有这个错误

  

无法生成验证码图像,内容已经输出。   这很可能是由于配置错误或发送了PHP错误   浏览

有人有想法吗?

注意:它适用于localhost wampserver但不适用于远程服务器

编辑:

来自验证码开源代码的代码生成php错误:

 protected function output()
{
    if ($this->canSendHeaders() || $this->send_headers == false) {
        if ($this->send_headers) {
            // only send the content-type headers if no headers have been output
            // this will ease debugging on misconfigured servers where warnings
            // may have been output which break the image and prevent easily viewing
            // source to see the error.
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
            header("Cache-Control: no-store, no-cache, must-revalidate");
            header("Cache-Control: post-check=0, pre-check=0", false);
            header("Pragma: no-cache");
        }

        switch ($this->image_type) {
            case self::SI_IMAGE_JPEG:
                if ($this->send_headers) header("Content-Type: image/jpeg");
                imagejpeg($this->im, null, 90);
                break;
            case self::SI_IMAGE_GIF:
                if ($this->send_headers) header("Content-Type: image/gif");
                imagegif($this->im);
                break;
            default:
                if ($this->send_headers) header("Content-Type: image/png");
                imagepng($this->im);
                break;
        }
    } else {
        echo '<hr /><strong>'
            .'Failed to generate captcha image, content has already been '
            .'output.<br />This is most likely due to misconfiguration or '
            .'a PHP error was sent to the browser.</strong>';
    }

    imagedestroy($this->im);
    restore_error_handler();

    if (!$this->no_exit) exit;
}

4 个答案:

答案 0 :(得分:3)

快速修复:

只需替换securimage.php文件中的canSendHeaders函数

protected function canSendHeaders() {

    if (headers_sent()) {

        // output has been flushed and headers have already been sent
        return false;
    } else if (strlen((string) ob_get_contents()) > 0) {
        // headers haven't been sent, but there is data in the buffer that will break image and audio data
        return false;
    }

    return true;
}

protected function canSendHeaders() {

    ob_get_clean();

    if (headers_sent()) {

        // output has been flushed and headers have already been sent
        return false;
    } else if (strlen((string) ob_get_contents()) > 0) {
        // headers haven't been sent, but there is data in the buffer that will break image and audio data
        return false;
    }

    return true;
}

答案 1 :(得分:0)

  

无法生成验证码图像,内容已经输出。这很可能是由于配置错误或PHP错误被发送到浏览器

我认为你缺少web.config中对dll的引用

答案 2 :(得分:0)

根据这个评估为false的声明判断:

if ($this->canSendHeaders() || $this->send_headers == false) {

在调用output函数之前,您已经将内容发送到浏览器。

为了能够使用该功能,您必须确保没有任何内容发送到浏览器;在打开<?php标记,没有echo语句等

之前没有空行或空格

答案 3 :(得分:0)

今天早上有一个问题与我一个月前完成的开发有关,今天部署时我已经忘记了我在securimage.php的顶部发出了回音,希望这可以帮助