CAPTCHA

Page 1 of 1

1 Replies - 507 Views - Last Post: 27 December 2010 - 02:14 AM

#1 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

CAPTCHA

Posted 02 May 2007 - 06:19 PM

Description:
Create the following SQL table:
CREATE TABLE captcha (
confirm_id char(32) DEFAULT '' NOT NULL,
session_id char(32) DEFAULT '' NOT NULL,
code char(8) DEFAULT '' NOT NULL,
PRIMARY KEY (session_id,confirm_id)
);

Modify the settings in the script.

This script uses TTF fonts to display the code. You can upload your own fonts to use. Just be sure to change $font_dir to wherever your fonts are stored. (A default font without rotation is used if none are available.)

There are two pages to this snippet. The first half is the captcha script. The second half is an example test page. Use the test page to understand how to implement the captcha.

This script originally inspired by the Anti-Spam ACP mod by EXreaction:
http://www.phpbb.com...hp?t=465600This is a simple captcha that generates an image based on a random string stored in a database. MySQL required.
<?php
//
// Settings
//
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$database = 'database';

$min_char = 4;
$max_char = 8;
$font_dir = './*.ttf';

//
// Initial start
//
mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($database);

session_start();

//
// Initiate image
//
header("content-type: image/png");
header("Cache-control: no-cache, no-store");

$width = rand(200, 300);
$height = rand(50, 70);
$img = imagecreate($width, $height);
$background = imagecolorallocate($img, rand(200, 255), rand(200, 255), rand(200, 255));

//
// Compile code
//
$array = array();
for ($i = 0; $i < $max_char; $i++)
{
  $alpha = chr(rand(1, 26) + 64);
  $numeric = rand(0, 9);

  $array[] = (rand(0, 1) == 0 || count($array) < $min_char) ? ((rand(0, 1) == 0) ? $alpha : $numeric) : '';
}

$code = str_replace('0', 'Z', implode('', $array));

//
// Query database
//
$confirm_id = (eregi('^[a-z0-9]{32}$', $_GET['id'])) ? $_GET['id'] : md5(uniqid($_SERVER['REMOTE_ADDR']));
$session_id = (eregi("^[a-z0-9]{32}$", session_id())) ? session_id() : '';
mysql_query("DELETE FROM captcha WHERE session_id = '$session_id'");
mysql_query("INSERT INTO captcha (confirm_id, session_id, code) VALUES ('$confirm_id', '$session_id', '$code')");

//
// Create background
//
for ($i = 0; $i < rand(5, 20); $i++)
{
  $color = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
  $loc_x = rand(-100, $width + 100);
  $loc_y = rand(-50, $height + 50);
  $end_x = rand(-100, $width + 100);
  $end_y = rand(-50, $height + 50);

  // Background shapes
  $shape = rand(1, 3);
  switch ($shape)
  {
    case 1:
      // Draw an ellipse
      imageellipse($img, $loc_x, $loc_y, rand(100, 400), rand(50, 400), $color);
    break;

    case 2:
      // Draw a rectangle
      imagerectangle($img, $loc_x, $loc_y, $end_x, $end_y, $color);
    break;

    case 3:
      // Draw a line
      imageline($img, $loc_x, $loc_y, $end_x, $end_y, $color);
    break;
  }
}

//
// Compile image
//
$x_move = $width / (strlen($code) + 1);
$current_pos = rand(0, $x_move / 2);

$less_rotate = array('N', 'Z', '6', '9');
for ($i = 0; $i < strlen($code); $i++)
{
  $size = rand(15, 20);
  $angle = (in_array($code[$i], $less_rotate)) ? rand(-15, 15) : rand(-30, 30);

  $x_pos = $current_pos + rand($size + 1, $x_move);
  $y_pos = $height/2 + rand(-8, 8);
  $current_pos = $x_pos;

  $fonts = glob($font_dir);
  $font = $fonts[array_rand($fonts)];
  $color = imagecolorallocate($img, rand(0, 127), rand(0, 127), rand(0, 127));

  if (!empty($font))
    imagettftext($img, $size, $angle, $x_pos, $y_pos, $color, $font, $code[$i]);
  else
    imagestring($img, 5, $x_pos, $y_pos, $code[$i], $color);
}

//
// Create image
//
imagepng($img);
imagedestroy($img);


/* ------ MySQL Query ------
CREATE TABLE captcha (
confirm_id char(32) DEFAULT '' NOT NULL,
session_id char(32) DEFAULT '' NOT NULL,
code char(8) DEFAULT '' NOT NULL, 
PRIMARY KEY  (session_id,confirm_id)
);
*/
?>

### TEST PAGE ###
<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');

session_start();

if (isset($_POST['submit']))
{
  $confirm_id = (eregi('^[a-z0-9]{32}$', $_POST['confirm_id'])) ? $_POST['confirm_id'] : '';
  $result = mysql_query("SELECT * FROM captcha WHERE confirm_id = '$confirm_id' LIMIT 1");
  $row = mysql_fetch_assoc($result);

  if (!$row || $_POST['confirm'] !== $row['code'])
  {
    echo '<b>Invalid Code:</b>';
  }
  else
  {
    $session_id = (eregi("^[a-z0-9]{32}$", session_id())) ? session_id() : '';
    mysql_query("DELETE FROM captcha WHERE session_id = '$session_id'");
    session_destroy();

    echo '<b>Valid:</b>';
  }

  echo $_POST['confirm'] . '|' . $row['code'] . '
';
}

$confirm_id = md5(uniqid($_SERVER['REMOTE_ADDR']));

?>
<img src="captcha.php?id=<?php echo $confirm_id; ?>">
<form method="post">
<input type="hidden" name="confirm_id" value="<?php echo $confirm_id; ?>">
<input type="text" name="confirm" size="30">
<input type="submit" name="submit" value="Submit">
</form>


Is This A Good Question/Topic? 0
  • +

Replies To: CAPTCHA

#2 shaikhaezaz80  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 27-December 10

Re: CAPTCHA

Posted 27 December 2010 - 02:14 AM

owsome yaar
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1