Connect with Expertise | Find Experts, Get Answers, Share Insights

PHP captcha class is not displaying the image

 
Join Date: Dec 2009
Location: Wherever i get a pc
Posts: 51
#1: Feb 8 '10
I just read a few tutorials about CAPTCHA from the net..I got the class captcha.php and a main file ..I pasted a few codes from the net but I cannot run the CAPTCHA as expected.The image is not being diaplayed....Instead I am getting the value from the alt in the image tag.

Here are my codes for the main file :
Expand|Select|Wrap|Line Numbers
  1. <?php session_start() ?>
  2. <html>
  3. <body>
  4. <form method="post" action="">
  5. <table bgcolor="#CCCCCC">
  6. <tr><td><input type="text" name="validator" id="validator" size="4" />
  7. <img src="captcha.php" alt="CAPTCHA image" align="top" /></td></tr>
  8.  
  9. <tr><th align="center"><input type="submit" value="Submit"></th></tr>    
  10. </table>
  11. </form>
  12. <?php
  13. if(isset($_POST["captcha"]))
  14. if (!empty($_POST['validator']) && $_POST['validator'] == $_SESSION['rand_code']) {
  15.     echo "Validated";
  16.     unset($_SESSION['rand_code']);
  17. }
  18. ?>
  19. </body>
  20. </html>

I tested the source of the image in the same directory as the captha.php and it works fine.

I will be highly obliged if anyone can help me in this ..Thanks

Markus's Avatar
E
C
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,516
#2: Feb 8 '10

re: PHP captcha class is not displaying the image


Sounds like your captcha.php isn't working as expected. However, without more information (maybe the class code?) we cannot help any further.
 
Join Date: Dec 2009
Location: Wherever i get a pc
Posts: 51
#3: Feb 8 '10

re: PHP captcha class is not displaying the image


my captcha.php is like the following :


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
  4. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
  5. header("Cache-Control: no-store, no-cache, must-revalidate"); 
  6. header("Cache-Control: post-check=0, pre-check=0", false);
  7. header("Pragma: no-cache"); 
  8.  
  9. function _generateRandom($length=6)
  10. {
  11.     $_rand_src = array(
  12.         array(48,57) //digits
  13.         , array(97,122) //lowercase chars
  14. //        , array(65,90) //uppercase chars
  15.     );
  16.     srand ((double) microtime() * 1000000);
  17.     $random_string = "";
  18.     for($i=0;$i<$length;$i++){
  19.         $i1=rand(0,sizeof($_rand_src)-1);
  20.         $random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));
  21.     }
  22.     return $random_string;
  23. }
  24.  
  25. $im = @imagecreatefromjpeg("captcha.jpg"); 
  26. $rand = _generateRandom(3);
  27. $_SESSION['captcha'] = $rand;
  28. ImageString($im, 5, 2, 2, $rand[0]." ".$rand[1]." ".$rand[2]." ", ImageColorAllocate ($im, 0, 0, 0));
  29. $rand = _generateRandom(3);
  30. ImageString($im, 5, 2, 2, " ".$rand[0]." ".$rand[1]." ".$rand[2], ImageColorAllocate ($im, 255, 0, 0));
  31. Header ('Content-type: image/jpeg');
  32. imagejpeg($im,NULL,100);
  33. ImageDestroy($im);
  34. ?>
Atli's Avatar
E
M
C
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,618
#4: Feb 9 '10

re: PHP captcha class is not displaying the image


Hey.

I would guess it has something to do with the imagecreatefrompng function. Try verifying that the image it creates is valid. (Something you should always try to do.)

Expand|Select|Wrap|Line Numbers
  1. header('Content-type: image/jpeg');
  2. $im = @imagecreatefromjpeg("captcha.jpg");
  3. if($im) {
  4.     // Display the CAPTCHA thing
  5. }
  6. else {
  7.     $_SESSION['captcha'] = false;
  8.     readfile('captcha-error.jpg');
  9. }
Now it will display the captcha-error.jpg image when a real captcha image can not be created. - Could be a error message, or it could even be a standard, temporary captcha that is displayed until you can fix the problem.

P.S.
You may want to check out reCAPTCHA.
Reply

Tags
captcha, php