Jump to content

captcha keeps saying correct answer any idea?


$php_mysql$

Recommended Posts

here is my form.php

 

<?php
session_start();		
	$digit1 = mt_rand(1,20);
        $digit2 = mt_rand(1,20);
        if( mt_rand(0,1) === 1 ) {
	$math = "$digit1 + $digit2";
	$_SESSION['answer'] = $digit1 + $digit2;
	} else {
	$math = "$digit1 - $digit2";
	$_SESSION['answer'] = $digit1 - $digit2;
	}	

if(count($_POST)){
	$postData = $_POST;		
	$files   = $_FILES;		
	$error  = ErrorCheck($postData, $_FILES);				
if(count($error) == 0) {		
		insertData($postData);			
	}
	}
?>

<form action="post_ads.php" name="postads" method="post" enctype="multipart/form-data"/>
Ad Description*: <br/>
	<textarea name="description" cols="40" rows="15"/><?php echo $postData['description'];?></textarea>
<?php
		if($error['description'] !='')
		{
			echo '<div class="error_msg">'.$error['description'].'</div>';
		}

?>
<br/>
What's <?php echo $math; ?> = <input name="answer" type="text" /><br />
<?php
		if($error['answer'] !='')
		{
			echo '<div class="error_msg">'.$error['answer'].'</div>';
		}

?>
<br/>
  <input type="submit" name="submit" value="Post"/>
</form>

 

and here is my function.php

 


function ErrorCheck($dataError, $_FILES){	

	   $errormsg = array();		
	   
	   if(!isset($_SESSION['answer']) || $_SESSION['answer'] != $_SESSION['answer'] )
		{
			$errormsg['answer'] = 'Seems like you are weak in math, Incorrect answer!';
		}else{
			 $errormsg['answer'] = 'Correct answer!';
			 unset($_SESSION['answer']);
		}

	  if($dataError['description'] == '') {
	   $errormsg['description'] = 'description is required!';
	   	}						
	  return $errormsg;
}

Link to comment
Share on other sites

so i tried this

 

function postErrorCheck($dataError, $_FILES, $_SESSION){	

	   $errormsg = array();		
	   
	  if (!isset($_SESSION['answer']) && $_SESSION['answer'] !==  $dataError['answer'])

		{
			$errormsg['answer'] = 'Seems like you are weak in math, Incorrect answer!';
			}else{
		    $errormsg['answer'] = 'Correct answer!';
			 printr($_SESSION['answer']);
			unset($_SESSION['answer']);
		}

 

still it keep saying correct answer

Link to comment
Share on other sites

did this now

/////////////////////////////////////////////
function postErrorCheck($dataError){	

	   $errormsg = array();		
	   
			if(isset($_SESSION['answer']) && $dataError['answer'] == '') {
		$errormsg['answer'] = 'You did not enter a answer!';
		}else if($_SESSION['answer'] !== $dataError['answer']) {
		$errormsg['answer'] = 'Your answer is wrong!';
		}else if($_SESSION['answer'] == $dataError['answer']) {
		unset($_SESSION['answer']);
		}

		if($dataError['description'] == '') {
	   $errormsg['description'] = 'description is required!';
	   	}						
	  return $errormsg;
}

still it says wrong answer to captcha

Link to comment
Share on other sites

what is wrong with this logic then?

 

if($dataError['answer'] == '') {
		$errormsg['answer'] = 'You did not enter a answer!';
		}else if(!isset($_SESSION['answer']) && $dataError['answer'] !== $_SESSION['answer']) {
		$errormsg['answer'] = 'You entered a wrong answer!';
		}else if($dataError['answer'] == $_SESSION['answer']) {
		unset($_SESSION['answer']);
		}

Link to comment
Share on other sites

What is wrong with it, is that you have not posted where $dataError['answer'] is coming from.  Therefore, we cannot tell how it is populated.

 

Also, if it is a dataError it should nor would ever in any possibility contain the same as SESSION['answer'] UNLESS you code your errors to the answer index of the SESSION array.

Link to comment
Share on other sites

lol sorry ok here it is

 


<?php
session_start();
error_reporting(0);
include 'functions.php';
//printr($_SESSION);
	$digit1 = mt_rand(1,20);
        $digit2 = mt_rand(1,20);
        if( mt_rand(0,1) === 1 ) {
	$math = "$digit1 + $digit2";
	$_SESSION['answer'] = $digit1 + $digit2;
	} else {
	$math = "$digit1 - $digit2";
	$_SESSION['answer'] = $digit1 - $digit2;
	}

if(count($_POST)){
	$postData = $_POST;			
	printr($postData);		
	$postData = cleanData($postData);
	$files   = $_FILES;		
	$error  = postErrorCheck($postData, $_FILES);			

if(count($error) == 0) {		
		insertData($postData);
		unset($postData);
		}
	}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<form action="try.php" name="postads" method="post" enctype="multipart/form-data"/>
Ad Description*: <br/>
	<textarea name="description" cols="40" rows="15"/><?php echo $postData['description'];?></textarea>
<?php
		if($error['description'] !='')
		{
			echo '<div class="error_msg">'.$error['description'].'</div>';
		}

?>

	<br/>

What's <?php echo $math; ?> = <input name="answer" type="text" /><br />
<?php
		if($error['answer'] !='')
		{
			echo '<div class="error_msg">'.$error['answer'].'</div>';
		}

?>

<br/>
  <input type="submit" name="submit" value="Post Ad"/>
</form>



<body>
</body>
</html>


and here is the functions.php 


/////////////////////////////////////////////
function postErrorCheck($dataError){	

	   $errormsg = array();		
	   
			if($dataError['answer'] == '') {
$errormsg['answer'] = 'You did not enter a answer!';
}else if(!isset($_SESSION['answer']) && $dataError['answer'] !== $_SESSION['answer']) {
$errormsg['answer'] = 'You entered a wrong answer!';
}else if($dataError['answer'] == $_SESSION['answer']) {
unset($_SESSION['answer']);
}

		if($dataError['description'] == '') {
	   $errormsg['description'] = 'description is required!';
	   	}						
	  return $errormsg;
}

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.