From the category archives:

Security

One thing I keep noticing is the use of background noise or clutter in CAPTCHAS. It’s now well known in the OCR (Optical Character recognition) field that background noise can be easily removed by computers. It’s basically useless at hindering spam bots.

It’s so easy that I was able to clean the following CAPTCHA up in only 20 lines of PHP code.

CAPTCHA

Here’s how…

At a glance, you can see the CAPTCHA’s background noise has a blue tint. CAPTCHA RGBLooking at the RGB value of the image in Photoshop, I can see that all parts of the background have a blue value higher than 180.

That’s the only piece of information needed to remove the background.

The code simply loops through every pixel of the image and checks the RGB value of it. If the blue (B) value is higher than 180, color it white.

Here’s the final image. The characters can now be easily separated and identified using OCR software.

CAPTCHA

So you can see why most background noise is basically useless in CAPTCHAS.

{ 0 comments }

Handling Errors in PHP

June 27, 2007

PHP errors are ugly and should never be shown to your users. Luckily with PHP we can handle errors our own way using the set_error_handler function.

I’ll start by defining the error handling function which will write the error to a log file and stop execution.


function handleError($level, $error, $file, $line)
{

  if ($logfile = fopen('log.txt', 'a'))
  {
    fwrite($logfile, date("F j, Y, g:i a") ." $error on line $line in $filen");
    fclose($logfile);
  }

  die('An error has occured.');

}

Then set it as the error handling function.

set_error_handler('handleError');

Trigger an error to see if it works.

echo 100 / 0;

You could also get your function to notify you via email. Or you could forget about logging, and just display better looking errors…


function handleError($level, $error, $file, $line)
{

  $errorhtml = '<div style="border: 4px solid #bfbfbf; font-size: 12px; font-family: arial, sans-serif; padding: 1px">';
  $errorhtml .= '<div style="background-color: #a3503f; color: #ffffff; padding: 3px;">ERROR</strong></div>';
  $errorhtml .= $error.'<br />';
  $errorhtml .= 'Line: '.$line.'<br />';
  $errorhtml .= 'File: '.$file;
  $errorhtml .= '</div>';

  echo $errorhtml;

}

php error

{ 0 comments }