扩展另一个类时,captcha类不起作用

时间:2022-09-02 12:35:39

I've got a script using a captcha class like this

我有一个使用像这样的验证码类的脚本

<?php
@session_start();

require str_replace('\\','/',dirname(__FILE__)) . "/example.extended.class.php";

class Ccaptcha extends ExampleExtended {

private $color1;
private $color2;
private $color3;
private $bgcolor;
private $bg_transparent = true;

private $final_width;

public function __construct() {
    parent::__construct();
    $this->final_width = $this->captcha_width;

    $this->color1   = $this->captcha_color1;
    $this->color2   = $this->captcha_color2;
    $this->color3   = $this->captcha_color3;
    $this->bgcolor = $this->captcha_colorbg;
}

public function CreateCaptcha() {

    // generate random number
    $randomnr = rand(1000, 9999);

    // MD5 it and store in session
    $_SESSION['commax_random_number'] = md5($randomnr);

    // Generate image
    $im = imagecreatetruecolor(200, 200);
    imagesavealpha($im, true);

    $color_1    = imagecolorallocate($im, 120, 180, 240);

    $color_2    = imagecolorallocate($im, 120, 180, 240);

    $color_3    = imagecolorallocate($im, 120, 180, 240);

    $background = imagecolorallocatealpha($im, 0, 0, 0, 127);


    imagefill($im, 0, 0, $background);
    imagestring($im, 100, 50, 50, $randomnr, $color_3);

    // prevent client side  caching
    header("Expires: Wed, 1 Jan 1997 00: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");

    //send image to browser
    header ("Content-type: image/png");
    imagepng($im);
    imagedestroy($im);

}

}

$captcha = new Ccaptcha();
$captcha->CreateCaptcha();

?>

Recently on a specific server install it stopped working, and showing a message "image cannot be shown because it contains errors.

最近在一个特定的服务器安装它停止工作,并显示一条消息“图像无法显示,因为它包含错误。

Strange is that if I remove the require for the example.extended.class.php and the remove all the parent::__construct() it works just fine. Well we would think that something is being outputted in the ExampleExtended and messing with the headers. Right I went to the ExampleExtended and removed everything from there. Just a class structure with nothing inside. Still no go.

奇怪的是,如果我删除example.extended.class.php的require并删除所有的parent :: __ construct()它就可以了。好吧,我们会认为在ExampleExtended中输出了一些东西并且弄乱了标题。我去了ExampleExtended并从那里删除了所有内容。只是一个没有任何内部的类结构。仍然没有去。

Also, the exact same script is running well on local server, and also on a few production servers...

此外,完全相同的脚本在本地服务器上以及在少数生产服务器上运行良好...

2 个解决方案

#1


1  

To debug this remove the call to header ("Content-type: image/png"); so you can see the errors that the system is issuing. You will want to ensure you have error reporting turned on like so:

要调试这个删除对标题的调用(“Content-type:image / png”);这样您就可以看到系统发出的错误。您需要确保启用错误报告,如下所示:

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

Also I wouldn't do this:

我也不会这样做:

require str_replace('\\','/',dirname(__FILE__)) . "/example.extended.class.php";

But try this instead:

但试试这个:

require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'example.extended.class.php';

DIRECTORY_SEPARATOR contains the correct slash for the underlying file system.

DIRECTORY_SEPARATOR包含底层文件系统的正确斜杠。

#2


0  

Well for the sake of completeness, I'll answer my own question, in case someone might bump into this problem.

那么为了完整起见,我会回答我自己的问题,万一有人可能碰到这个问题。

What was happening was that when calling the second class, that file had been opened and saved in UTF-8 mode with BOM, but all my files are UTF-8 without BOM.

发生的事情是,当调用第二个类时,该文件已经打开并以带有BOM的UTF-8模式保存,但我的所有文件都是UTF-8而没有BOM。

So there was a encoding issue with both files. All I have to do was to open the called class and convert it to UTF-8 without BOM and that was it!

因此两个文件都存在编码问题。我所要做的就是打开被调用的类并将其转换为没有BOM的UTF-8,就是这样!

#1


1  

To debug this remove the call to header ("Content-type: image/png"); so you can see the errors that the system is issuing. You will want to ensure you have error reporting turned on like so:

要调试这个删除对标题的调用(“Content-type:image / png”);这样您就可以看到系统发出的错误。您需要确保启用错误报告,如下所示:

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

Also I wouldn't do this:

我也不会这样做:

require str_replace('\\','/',dirname(__FILE__)) . "/example.extended.class.php";

But try this instead:

但试试这个:

require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'example.extended.class.php';

DIRECTORY_SEPARATOR contains the correct slash for the underlying file system.

DIRECTORY_SEPARATOR包含底层文件系统的正确斜杠。

#2


0  

Well for the sake of completeness, I'll answer my own question, in case someone might bump into this problem.

那么为了完整起见,我会回答我自己的问题,万一有人可能碰到这个问题。

What was happening was that when calling the second class, that file had been opened and saved in UTF-8 mode with BOM, but all my files are UTF-8 without BOM.

发生的事情是,当调用第二个类时,该文件已经打开并以带有BOM的UTF-8模式保存,但我的所有文件都是UTF-8而没有BOM。

So there was a encoding issue with both files. All I have to do was to open the called class and convert it to UTF-8 without BOM and that was it!

因此两个文件都存在编码问题。我所要做的就是打开被调用的类并将其转换为没有BOM的UTF-8,就是这样!