0

My PHP captcha doesnt work on my OVH server.

Everything was fine on localhost with wamp. After un upload, the captcha is simply not displayed on my website.

I don't know what i'm doing wrong.

My captcha.php is included in my form like : <img src="captcha.php" alt="CAPTCHA" class="captcha-image">

PS : php-gd is already installed and up to date. PHP version : 7.0.33

captcha.php :

session_start();

$permitted_chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ';

function generate_string($input, $strength = 10) {
    $input_length = strlen($input);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        $random_character = $input[mt_rand(0, $input_length - 1)];
        $random_string .= $random_character;
    }

    return $random_string;
}

$image = imagecreatetruecolor(200, 50);

    imageantialias($image, true);

$colors = [];

$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);

for($i = 0; $i < 5; $i++) {
$colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}

imagefill($image, 0, 0, $colors[0]);

for($i = 0; $i < 10; $i++) {
    imagesetthickness($image, rand(2, 10));
    $line_color = $colors[rand(1, 4)];
    imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $line_color);
}

$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$textcolors = [$black, $white];

$fonts = [dirname(__FILE__).'\fonts\Acme.ttf', dirname(__FILE__).'\fonts\Ubuntu.ttf', dirname(__FILE__).'\fonts\Merriweather.ttf', dirname(__FILE__).'\fonts\PlayfairDisplay.ttf'];

    $string_length = 6;
$captcha_string = generate_string($permitted_chars, $string_length);

$_SESSION['captcha_text'] = $captcha_string;

    for($i = 0; $i < $string_length; $i++) {
    $letter_space = 170/$string_length;
        $initial = 15;

    imagettftext($image, 24, rand(-15, 15), $initial + $i*$letter_space, rand(25, 45), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]);
}

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>

Local : https://i.stack.imgur.com/Pa6lW.png Remote : https://i.stack.imgur.com/BjvKv.png

shaax
  • 306
  • 3
  • 18
  • Can you right click on the (broken) captcha image, copy `image link address` and paste it here (for both local and remote) – saibbyweb Feb 09 '19 at 18:05
  • A likely scenario here is that the page might be generating a php warning/notice, before the image - so the browser is getting a corrupt image. I'd be inclined to run the page with the last two lines commented out and check whether or not any php warnings are being generated, and add an echo 'test'; to make sure that you get to the end of the file - and see if that shows anything up – Mantis Support Feb 09 '19 at 18:27
  • @saibbyweb : http://localhost/captcha.php?1549736811543 remote : http://www.example.com/captcha.php?1549733788640 – shaax Feb 09 '19 at 18:28
  • @MantisSupport : Same result with the lines comment, and i get the echo test – shaax Feb 09 '19 at 18:34

2 Answers2

0

I think you are missing GD extension on your OVH server. Check logs to confirm. Your server sends error to the browsers, but since browser expects images it shows missing/corrupted image icon.

You can install GD using one of these commands (depending on your PHP version)

sudo apt-get install php7.0-gd
# or
sudo apt-get install php7.1-gd
# or
sudo apt-get install php7.2-gd

Check install php70-gd on ubuntu for more details how to install GD

mleko
  • 11,650
  • 6
  • 50
  • 71
  • As i said : "PS : php-gd is already installed and up to date. PHP version : 7.0.33". So, the command return me "php7.0-gd is already the newest version (7.0.33-0+deb9u1) ". But i'll try to install PHP 7.2 right now – shaax Feb 09 '19 at 18:44
  • @shaax do you have it enabled in your fpm/apache? Can you show phpinfo() dump? Are there any warnings/errors? What is the actual output when you try to open captcha image? – mleko Feb 09 '19 at 18:46
  • Ok i'm just doing something crap with update to php 7.2. I reinstall a fresh Debian 9, install PHP modules, and post you a phpinfo() dump soon. Thank you for helping me – shaax Feb 09 '19 at 19:16
  • Okay, it works... Almost. Now, i just see the background of the captcha, not the fonts. Maybe something is wrong with my font path, but why only on the server... I investigate – shaax Feb 09 '19 at 20:30
0

Ok, got it. The captcha is working on PHP 7.2 only. Wamp have 7.2 by default, debian 9 have 7.0.

I've just properly installed 7.2 and everything is fine. My path problem was solved with / instead of \ (ty Windows...).

Thank you all.

shaax
  • 306
  • 3
  • 18
  • 1
    nice spot - just as a FYI - it's going to be safe to always use / . If ever you want to display the path, can use the constant DIRECTORY_SEPARATOR . No need in this case as / is simpler and quicker ;) – Mantis Support Feb 11 '19 at 00:45