Jump to content

[SOLVED] Captcha?


Cory94bailly

Recommended Posts

Well I have an old captcha script that a friend made for me..

 

It worked on my old free host but for some reason, it's not working now...

 

Captcha.php:

 

 

 

<?
session_start();
    function random_number($length=3) {
        $chars = "123456789abcdefghijklmnpqrstuvwxyz";
        $code = "";
        while (strlen($code) < $length) {
            $code .= $chars[mt_rand(0,strlen($chars))];
        }
        return $code;
    }
$im = imagecreate(26, 15);
$string = random_number();
$_SESSION['string'] = $string;
$bg = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

	for($i=0;$i<3;$i++){
		$y = 20;
            imagestring($im, 5, 0, 0, $string, $black);
		$x = $x + 10;
	}
header('Content-type: image/gif');
imagegif($im);
?>

 

 

Form.php:

 

<html>
<head>
<title>Test with Captchas</title>
</head>
<body>
Note: All 0's are zeros, there are no letter o's.
<form action="test.php" method="POST">
<table border="0">
<tr><td><img src="captcha.php"></td></tr>
<tr><td>Captcha:</td><td><input type="text" name="captcha"><br></td></tr>
</table>
<input type="submit" name="enter" value="enter">
</form>
</body>
</html>

 

 

Test.php:

 

<?
session_start();
//if the string in the image exist
if (isset($_SESSION['string'])) {
//if the captcha you write and the captcha in the image are the same
if ($_POST['captcha'] == $_SESSION['string']) {
//show "correct"
echo 'Correct!';
//if not
} else {
//show incorrect
echo 'Incorrect!';
//if the string in the image dont exist
}} else {
//shows "error"
echo 'Error!!!';
} 
session_end();
?>

 

 

 

 

Also, would there be any other better way to do this? Like how do I make the letters harder to see and stuff?

Link to comment
https://forums.phpfreaks.com/topic/107371-solved-captcha/
Share on other sites

Never mind, I found a new script..

 

<?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 will be 75% of the image height
      $font_size = $height * 0.75;
      $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
      //Set the colours
      $background_color = imagecolorallocate($image, 255, 255, 255);
      $text_color = imagecolorallocate($image, 20, 40, 100);
      $noise_color = imagecolorallocate($image, 100, 120, 180);
      //Generate random dots in background
      for( $i=0; $i<($width*$height)/3; $i++ ) {
         imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
      }
      //Generate random lines in background
      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);
      }
      //Create textbox and add text
      $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');
      //Output captcha image to browser
      header('Content-Type: image/jpeg');
      imagejpeg($image);
      imagedestroy($image);
      $_SESSION['security_code'] = $code;
   }

}

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

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

?>

Link to comment
https://forums.phpfreaks.com/topic/107371-solved-captcha/#findComment-550485
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.