I have a working CAPTCHA script that I'm using myself on my website. I gave a copy to my friend for his website but for some reason it doesn't work on his shared host. I've turned on all error reporting but I get nothing at all, just a blank page when I open the file.
The only thing left
I can think of is that my friends host's php installation doesn't include the GB library. But then again, that should give some error of calling functions that doesn't exist. Can anybody think of another reason it wouldn't work?
I'll include the code, if you feel it would help to look at it.
PHP Code:
<?php
error_reporting(E_ALL | E_STRICT);
session_cache_expire(15);
session_start();
/*
* Modified by Mattias N. (2011-02-02)
* Information from original script is below.
*
* =====================================================================
*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/
class CaptchaSecurityImage {
// Some minor configurations
var $font = 'TravelingTypewriter.ttf'; // Font to use in image
var $session_name = 'security_code'; // Name of the session where code
// is stored. Accessed through
// $_SESSION[$session_name]
var $width = 200;
var $height = 50;
var $length = 4;
function generateCode($length) {
/* list all possible characters, similar looking characters and lowercase characters have been removed */
$allowed = '23456789ABCDEFGHJKLMNPQRSTWXZ';
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= substr($allowed, mt_rand(0, strlen($allowed)-1), 1);
}
return $code;
}
function random_color($image, $min = 0, $max = 255) {
return imagecolorallocate($image, mt_rand($min,$max), mt_rand($min,$max), mt_rand($min,$max));
}
function show() {
// If there is a previous code, reset it
$code = $this->generateCode($this->length);
// font size will be 75% of the image height
$font_size = $this->height * 0.75;
// create new empty image
$image = @imagecreate($this->width, $this->height) or die('Cannot Initialize new GD image stream');
// set some colors
$background_color = $this->random_color($image, 0, 90);
// $text_color = imagecolorallocate($image, 50, 50, 50);
$noise_color = imagecolorallocate($image, 255, 255, 255); // White
// generate random dots in background
for ($i = 0; $i < ($this->width*$this->height)/100; $i++) {
imagefilledellipse($image,
mt_rand(0,$this->width), // x coordinate
mt_rand(0,$this->height), // y coordinate
mt_rand(1,5), // width
mt_rand(1,5), // height
$noise_color // color
);
}
/* generate random lines in background */
// for( $i=0; $i<($width*$height)/500; $i++ ) {
// imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
// }
$x_step = $this->width / 4;
for ($i=0; $i < strlen($code); $i++) {
$letter = $code{$i};
$angle = mt_rand(-30, 30);
$letterbox = imagettfbbox($font_size, $angle, $this->font, $letter);
$x = $i * $x_step + ($this->width / 4 - $letterbox[4]) / 2;
$y = ($this->height * 0.6 - $letterbox[5]) / 2 + $this->height * 0.2;
imagettftext($image, $font_size, $angle, $x, $y, $this->random_color($image, 120, 255), $this->font, $letter);
}
// output image to browser
header('Content-Type: image/jpeg');
imagejpeg($image);
// destroy image
imagedestroy($image);
// set session variable
$_SESSION[$this->session_name] = $code;
}
}
$captcha = new CaptchaSecurityImage();
$captcha->show();
?>