Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Captcha

Featured Replies

Hi,

 

I have this captcha set up on my website form and since I have re-uploaded files the image is no longer visible, can somebody please confirm that this file plus the font is all I need in my root folder server side or am I missing something simple?

 

<?php
session_start();

class CaptchaSecurityImages {

   var $font = 'monofont.ttf';

   function generateCode($characters) {

       $possible = '23456789bcdfghjkmnpqrstvwxyz';
       $code = '';
       $i = 0;
       while ($i < $characters) {
           $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
           $i++;
       }
       return $code;
   }

   function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
       $code = $this->generateCode($characters);

       $font_size = $height * 0.75;
       $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');

       $background_color = imagecolorallocate($image, 255, 255, 255);
       $text_color = imagecolorallocate($image, 20, 40, 100);
       $noise_color = imagecolorallocate($image, 100, 120, 180);

       for( $i=0; $i<($width*$height)/3; $i++ ) {
           imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
       }

       for( $i=0; $i<($width*$height)/150; $i++ ) {
           imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
       }

       $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
       $x = ($width - $textbox[4])/2;
       $y = ($height - $textbox[5])/2;
       imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');

       header('Content-Type: image/jpeg');
       imagejpeg($image);
       imagedestroy($image);
       $_SESSION['security_code'] = $code;
   }

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>

 

 

 

 

Thanks

Could be a number of things

- Permissions issue

- GD not installed on server

- Error in script being output and causing image to malform

 

Out of the three, I'm going to say it's likely to be the last one

 

Try commenting out these two lines

        header('Content-Type: image/jpeg');
       imagejpeg($image);

and see if you get any errors showing. You could also check your error_log file to see if there are any errors appearing in there

 

  • Author

Could be a number of things

- Permissions issue

- GD not installed on server

- Error in script being output and causing image to malform

 

Out of the three, I'm going to say it's likely to be the last one

 

Try commenting out these two lines

        header('Content-Type: image/jpeg');
       imagejpeg($image);

and see if you get any errors showing. You could also check your error_log file to see if there are any errors appearing in there

 

 

I commented out the above lines of code and re-uploaded, still nothing and there are no errors showing. I downloaded the image from the website, I have attached a screenshot of the message I received - the 'CaptchaSecurityImages.php' is server side so I am at a loss as to why this is saying differently!?

 

Thanks

post-19708-0-87480800-1304166655_thumb.png

I can't see the problem, but I'd suggest you upgrade to ReCaptcha or an alternative.

 

Captcha has been broken and doesn't not guard against spam anymore.

 

ReCaptcha has an easy to use Class already made on their website.

 

An alternative could be a Question/Answer challenge (eg, "What colour is the sky?" or "What is this website's name?").

  • Author

I can't see the problem, but I'd suggest you upgrade to ReCaptcha or an alternative.

 

Captcha has been broken and doesn't not guard against spam anymore.

 

ReCaptcha has an easy to use Class already made on their website.

 

An alternative could be a Question/Answer challenge (eg, "What colour is the sky?" or "What is this website's name?").

 

 

I found the captcha image was broken so that is working for now, however I will try re-captcha...thanks

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.