Jump to content

Captcha questions


Irresistable

Recommended Posts

I'm wanting to create this captcha into an average sort of difficulty. At the moment, it's pretty plain, a background with some text. How could I improve the difficulty maybe with more lines also with letters aswell as numbers.

Here is the php of the captcha (This is used in <img> tags on the form)

<?php
session_start();
header ("Content-type: image/png");

$rno = rand(1000,99999);
$_SESSION['ckey'] = md5($rno);

$img_handle = imageCreateFromPNG("bg1.PNG");
$color = ImageColorAllocate ($img_handle, 0, 0, 0);
ImageString ($img_handle, 5, 20, 13, $rno, $color);
ImagePng ($img_handle);
ImageDestroy ($img_handle);

?>

 

Also, how can I create a multiplication sum, random out of lets say 5 choices.

1 + 1, 2 + 1, 2 - 1, 0 + 1, 0 + 2 for example.

No background, but so the field would come up like...

1 + 1 = ? Then I'll have a textbox for the answer. The answer is put in a session key like the captcha would be.

 

Can anyone help me out?

Thanks.

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

To be honest, there are plenty of tutorials that will provide much more in-depth help for your question, using tutorials saves everyone time as you get the info on the spot, and we dont have to write a mini-tutorial for you :P.

 

Regarding making the Capcha Harder to Read (Not truly Necessary, if you make it too hard i will personally hate you cuz there are so many silly sites making it impossible to submit a form on the first try). Anyway ramble over;

http://www.ebrueggeman.com/blog/advanced-php-captcha/

 

Regarding making a different form of capcha, ie. the Mathematical Capcha;

 

I have created this for you:

<?php

// Start the Session
session_start();

// Check if a Capcha Result has been set in this session.
if(isset($_SESSION['capcha_result'])){

// Check if the Capcha code was entered in the form
if(isset($_POST['capchacode'])){

	// Check if the code matches
	if($_SESSION['capcha_result'] == $_POST['capchacode']){

		// It does, So display what you want here if successfull (or do functions etc).
		echo("CORRECT!");

		// Unset the cpacha code for this session (Not needed anymore).
		unset($_SESSION['capcha_result']);

		// Exit (Prevents execution of further code, this is here so it doesnt render the form again.).
		exit();

	// If code doesn't match
	}else{
		echo("FAIL! - Answer: ".$_SESSION['capcha_result']);
		unset($_SESSION['capcha_result']);
		exit("<br />"."<a href='./test.php'>Try Again?</a>");
	}
}else{
	// If the code wasn't entered, unset the capcha result so they have to reload the page.
	unset($_SESSION['capcha_result']);
}
}

#== CAPCHA FUNCTION ==#

// For Random Operations
$types = array(
"plus",
"minus",
"multiplied",
"divided"
);

// Only necessary for Multiply/Divide Operations
$Numbers = Array(
0,
1,
2,
5,
10,
20,
50,
100,
200,
500,
1000
);

// Plus/Minus or Divided/Multiplied
$type = $types[rand(0,3)];

// Get the random Numbers
$num1 = rand(1,100); // Any Number

// This number if, multiply or divide is set, must be relatively easy to work out (we dont want people to use calculators).
$num2 = ($type == ("multiplied" || "divided"))? $Numbers[rand(0,7)] : rand(1,100);

// The Fun Begins.
Switch($type){
default:
	// Plus

	$result = $num1 + $num2; // Set result (to Match against).
	$_SESSION['capcha_result'] = $result; // Save result to SESSION
	echo("What is ".$num1." + ".$num2."?"); // Echo the Question (You could also save this to a variable to display easier in a template or such.)

break;

case "minus":

	$result = $num1 - $num2;
	$_SESSION['capcha_result'] = $result;
	echo("What is ".$num1." - ".$num2."?");

break;

case "multiplied":

	$result = $num1 * $num2;
	$_SESSION['capcha_result'] = $result;
	echo("What is ".$num1." x ".$num2."?");

break;

case "divided":

	$result = $num1 / $num2;
	$_SESSION['capcha_result'] = $result;
	echo("What is ".$num1." Divided by ".$num2."?");

break;
}

// Capcha Form/Question.
echo("<br /><form action='./test.php' method='post'><input type='text' size='8' name='capchacode' /><br /><input type='submit' /></form>");


?>

 

You can run this code as "test.php" on its own, without the image functions you can easier see how to incororate this into your own code. This i think is quite good as it can use Divide/Multiply as well as Plus/Minus, and should be easily understood and calculated by most people with at least a level 1 math grade (multiply by 10's and 5's).

 

-CB-

Link to comment
https://forums.phpfreaks.com/topic/186797-captcha-questions/#findComment-986426
Share on other sites

Babes, I got a question when I loaded it as it is.

"What is 17 Divided by 5?"

That is not possible?! Others like that too..

Also division by 0, brings up an

"Warning: Division by zero in /home/jeanie/public_html/recaptcha/test.php on line 99"

Possibly the same for times.

Link to comment
https://forums.phpfreaks.com/topic/186797-captcha-questions/#findComment-986442
Share on other sites

np, just a few more if statements could make that multiply/division work a treat (just make it check for multiples of 5 of each number to make it real easy :P).

 

Also to get rid of that division by 0, put a @ symbol after the = symbol on the specified lines :P.

 

And np, Happy New Year!

-CB-

Link to comment
https://forums.phpfreaks.com/topic/186797-captcha-questions/#findComment-986474
Share on other sites

Just generate the captcha image as usual, but use the Equation as the Text String.

 

So, image.php is like this:

<?php

// Start the Session (Needed at Beggining).
session_start();

header ("Content-type: image/png");

// Declare all the functions first.

// Function borrowed from php.net comments.
function is_true_float($val){
    if( is_float($val) || ( (float) $val > (int) $val || strlen($val) != strlen( (int) $val) ) && (int) $val != 0  ) return true;
    else return false;
}

#== CAPCHA FUNCTION ==#

function generate_captcha(){

// For Random Operations
$types = array(
   "plus",
   "minus",
   "multiplied",
   "divided"
);

// Only necessary for Multiply/Divide Operations
$Numbers = Array(
   0,
   1,
   2,
   5,
   10,
   20,
   50,
   100,
   200,
   500,
   1000
);

// Plus/Minus or Divided/Multiplied
$type = $types[rand(0,3)];

// Get the random Numbers
$num1 = rand(1,100); // Any Number

// This number if, multiply or divide is set, must be relatively easy to work out (we dont want people to use calculators).
$num2 = ($type == ("multiplied" || "divided"))? $Numbers[rand(0,7)] : rand(1,100);

// The Fun Begins.
Switch($type){
   default:
      // Plus
      
      $result = $num1 + $num2; // Set result (to Match against).
      $q = $num1." + ".$num2;
      
   break;
   
   case "minus":
      
      $result = $num1 - $num2;
      $q = $num1." - ".$num2;
      
   break;
   
   case "multiplied":
      
      // Some extra checks to make multiply/divide a little easier 
      while(is_true_float($num1 * $num2) == true){
            
            $num1++;
      }
      
      $result = $num1 * $num2;
      $q = $num1." x ".$num2;
      
   break;
   
   case "divided":
      
      // Some extra checks to make multiply/divide a little easier 
      while(is_true_float($num1 * $num2) == true){
            
            $num1++;
      }
      
      $result = $num1 / $num2;
      $q = $num1." ÷ ".$num2;
      
   break;
}

// Return Question and Result.
return array($q, $result);

}

$capcha = generate_capcha();

// Make image
$_SESSION['ckey'] = md5($capcha[1]);

// Image File
$img_handle = imageCreateFromPNG("capcha.PNG");

// Image Start Color
$color = ImageColorAllocate ($img_handle, 0, 0, 0);

// Add Capcha String
ImageString ($img_handle, 5, 20, 13, $capcha[0], $color);

// Display The Image
ImagePng ($img_handle);

// Destroy The Image
ImageDestroy ($img_handle);

?>

 


 

So the variable your form is working with is:

$_SESSION['ckey']

 

That should be all you need.

 

Take a look at the following code for a half-decent sanity check for the form.

 

form.php

<?php

// Check if a Capcha Result has been set in this session.
if(isset($_SESSION['capcha_result'])){
   
   // Check if the Capcha code was entered in the form
   if(isset($_POST['capchacode'])){
      
      // Check if the code matches
      if($_SESSION['ckey'] == md5($_POST['capchacode'])){
         
         // It does, So display what you want here if successfull (or do functions etc).
         echo("CORRECT!");
         
         // Unset the capcha code for this session (Not needed anymore).
         unset($_SESSION['ckey']);
         
         // Exit (Prevents execution of further code, this is here so it doesnt render the form again.).
         exit();
         
      // If code doesn't match
      }else{
         echo("FAIL! - Answer: ".$_SESSION['ckey']);
         unset($_SESSION['ckey']);
         exit("<br />"."<a href='./form.php'>Try Again?</a>");
      }
   }else{
      // If the code wasn't entered, unset the capcha result so they have to reload the page.
      unset($_SESSION['ckey']);
   }
}

// No captcha has been entered or submitted, lets make the captcha image here.


// Capcha Form/Question.
echo("<br />
<form action='./form.php' method='post'>
<img src='image.php' />
<input type='text' size='8' name='capchacode' />
<br />
<input type='submit' />
</form>");

?>

 

This code is untested, but should work flawlessy (although written in this tiny little text box in safe-mode :P), should be ok.

 

-CB-

Link to comment
https://forums.phpfreaks.com/topic/186797-captcha-questions/#findComment-988198
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.