Jump to content

CAPTCHA class


cromine87

Recommended Posts

I have been trying to create this captcha class and then display a captcha image to a webpage. My code has lots of issues I'm sure, but I was wondering if anyone could help me figure out what I am doing wrong. My current code is as follows:

 

 

<?php
 
class Captcha
{
private $teststring = '';
private $strlength = 0;
private $backcolor = 0;
private $textcolor = 0;
private $image = null;
 
function __construct($w=200,$l=50)
{
$this->image = imageCreate($w,$l);
}
 
private function defineCaptcha()
{
$captchastr = "";
 
for($i=1;$i<=$strlength;$i++)
{
$textornumber = rand(1,3);
 
if($textornumber == 1)
{
$captchastr .= chr(rand(49,57));
}
 
if($textornumber == 2)
{
$captchastr .= chr(rand(65,78));
}
 
if($textornumber == 3)
{
$captchastr .= chr(rand(80,90));
}
}
}
 
private function textToImage()
{
$randcolR = rand(100,230);
$randcolG = rand(100,230);
$randcolB = rand(100,230);
 
//initialize image $captcha is handle dimensions 200,50
$captcha = imageCreate(200,50);
$backcolor = imageColorAllocate($captcha, $randcolR, $randcolG, $randcolB);
$textcolor = imageColorAllocate($captcha, ($randcolR - 20), ($randcolG - 20), ($randcolB - 20));
 
for($i=1;$i<=$strlength;$i++)
{
$clockorcounter = rand(1,2);
 
if ($clockorcounter == 1)
{
$rotangle = rand(0,45);
}
 
if ($clockorcounter == 2)
{
$rotangle = rand(315,360);
}
 
//$i*25 spaces the characters 25 pixels apart
imagettftext($captcha,rand(14,20),$rotangle,($i*25),30,$textcolor,FONT,substr($captchastr,($i-1),1));
}
 
}
 
private function drawEllipses()
{
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$textcolor);
}
 
for($i=1; $i<=4;$i++)
{
imageellipse($captcha,rand(1,200),rand(1,50),rand(50,100),rand(12,25),$backcolor);
}
}
 
private function finishingTouch()
{
$_SESSION['testString'] = $this->testString;
//Send the headers (at last possible time)
ob_start();
//header('Content-type: image/png');
//Output the image as a PNG 
 
//Delete the image from memory
//imageDestroy($this->image);
imagePNG($this->image);
$output = ob_get_contents();
ob_end_clean();
return  $output;
}
 
public function makeImage()
{
$this->defineCaptcha();
$this->textToImage();
$this->drawEllipses();
$this->finishIt();
}
}
?>
<?
session_start();
 
$myCaptcha = new Captcha();
 
//sentinal variable
$continue = true;
 
if(isset($_POST['submit']))
{
if($_POST['code'] == $_SESSION['captchastr'])
{
echo "Success!!";
$continue = false;
}
else
{
echo "Please try again!!";
$continue = true;
}
}
 
?>
<?
if($continue == true)
{
//this is the code to produce the image in the HTML
// calls the public method that creates validation image
$image = $myCaptcha->makeImage();
$image = base64_encode($image);
//heredoc to hold html output
//$image = $_SESSION['image'];
$outString = <<<EOF
<html>
<head>
<title>Reverse Turing Test</title>
</head>
<body>
<img src="data:image/png;base64, {$image}">
<form name="myform" action="" method="POST">
<input type="text" name="userInput" value="" /><br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
EOF;
echo $outString;
}
?>
Edited by cromine87
Link to comment
Share on other sites

Well if you are very new to PHP, congratulations, you are able to write decent OOPHP in just a short period of time. Keep up the excellent work, you will improve from an amateur to professional coder soon.

 

A friendly advice though, use netbeans or eclipse to load your script and it can detect syntax errors immediately for you. In most cases the blank page is caused by syntax errors, in a large program it may not be easily detectable. If there is no syntax error, you will have to enable error reporting and track the problem deep down.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.