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.
Also, the exact same script is running well on local server, and also on a few production servers…
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.
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!