cromine87 Posted April 15, 2013 Share Posted April 15, 2013 (edited) 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 April 15, 2013 by cromine87 Quote Link to comment https://forums.phpfreaks.com/topic/276994-captcha-class/ Share on other sites More sharing options...
Jessica Posted April 15, 2013 Share Posted April 15, 2013 And the problem is? Quote Link to comment https://forums.phpfreaks.com/topic/276994-captcha-class/#findComment-1424972 Share on other sites More sharing options...
cromine87 Posted April 16, 2013 Author Share Posted April 16, 2013 I'm not sure, It's not working for me on my machine. Trying to figure out what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/276994-captcha-class/#findComment-1424973 Share on other sites More sharing options...
Jessica Posted April 16, 2013 Share Posted April 16, 2013 Define "not working". Did you write the code? This forum is for help with code you wrote. Quote Link to comment https://forums.phpfreaks.com/topic/276994-captcha-class/#findComment-1424974 Share on other sites More sharing options...
cromine87 Posted April 16, 2013 Author Share Posted April 16, 2013 Not working meaning nothing appears in the browser. This is my code that I wrote, I'm very new at PHP and don't know this stuff very well at all, I apoligize. Quote Link to comment https://forums.phpfreaks.com/topic/276994-captcha-class/#findComment-1424977 Share on other sites More sharing options...
jcbones Posted April 16, 2013 Share Posted April 16, 2013 You need to make sure error_reporting is turned on E_ALL, and display errors is on. You cannot output an image, and HTML in the same script. You will have to separate them. Quote Link to comment https://forums.phpfreaks.com/topic/276994-captcha-class/#findComment-1424978 Share on other sites More sharing options...
Hall of Famer Posted April 16, 2013 Share Posted April 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276994-captcha-class/#findComment-1425016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.