【问题标题】:Failed to generate captcha image, content has already been output生成验证码图片失败,内容已输出
【发布时间】:2012-05-30 04:46:01
【问题描述】:

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

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

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

当进入这个url时直接用作图片src

securimage_show.php?sid=fa5e1eb19c3a534885632e

我有这个错误

验证码图片生成失败,内容已输出。 这很可能是由于配置错误或 PHP 错误发送到 浏览

有人知道吗?

注意:它适用于本地 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;
}

【问题讨论】:

  • 你能提供一些你的代码吗?
  • 我会看到,但它适用于本地主机,但不适用于远程服务器
  • 您忘记包含评估为假的关键if 语句...
  • @jeroen:已编辑(添加整个函数)
  • @khaled_webdev 嗨,Khaled,您还有这个问题吗?如果是这样,我可以为您提供解决方案。我是 Securimage 验证码的作者。

标签: php apache google-chrome captcha


【解决方案1】:

快速修复:

只需替换 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;
}

【讨论】:

    【解决方案2】:

    今天早上我在一个月前完成的开发中遇到了同样的问题,今天部署时我忘记了我在 securimage.php 的顶部放置了一个 echo,希望这可以帮助

    【讨论】:

    • 感谢您,我刚刚删除了错误消息的条件,并且像魅力一样毫无问题地工作,也许不是最好的解决方案,但它对我有用。是的,我必须验证任何回声或空行,例如推荐 jeron,作者。
    【解决方案3】:

    验证码图片生成失败,内容已输出。这很可能是由于配置错误或向浏览器发送了 PHP 错误

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

    【讨论】:

    • 很遗憾它是共享服务器,无法配置
    • web.config?他的问题是关于 PHP 的,听起来你指的是 .NET 网络应用程序......
    【解决方案4】:

    从这个评估为false的语句判断:

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

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

    为了能够使用该功能,您必须确保没有向浏览器发送任何内容;在打开&lt;?php 标签之前没有空行或空格,没有echo 语句等。

    【讨论】:

    • @khaled_webdev 可以是简单的空格或换行符,会被 wamp 而不是服务器忽略。
    • 我已将此条件 if ($this->canSendHeaders() || $this->send_headers == false) 更改为 if(1==1) 以忽略它。这样做有副作用吗?
    猜你喜欢
    • 2010-11-04
    • 2018-09-27
    • 1970-01-01
    • 2011-09-23
    • 2021-10-12
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多