Recaptcha Not woking? Undefined index: recaptcha_challenge_field - php

I have try many times but the recaptcha still doesn't work and always say Undefined index: recaptcha_challenge_field
But i was followed the instruction ...
Hare is my coding ... i using yii framework
IN php :
<?php
<form id="registration" method="post" action="<?php echo Yii::app()->getHomeUrl(); ?>?r=Register/Registration">
//some input detail
//at the bottom the recaptcha code
<?php
require_once('captcha/recaptchalib.php');
$publickey = "the_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
</form>
?>
THEN in the controller
public function actionRegistration(){
require_once('captcha/recaptchalib.php');
$privatekey = "the_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
echo "Your code here to handle a successful verification";
}
}
Any suggestion to make it working in this yii project?

The $_POST variable probably does not have what you are looking for.
When you have this kind of problem, the best thing to do is to call var_dump() to see what is being added to $_POST, for example:
require_once('captcha/recaptchalib.php');
$privatekey = "the_private_key";
//var dump for debugging:
echo '<pre>';
var_dump($_POST);
echo '</pre>';
exit;
//now the rest of your code, as before:
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);

Related

Code is not working when reCaptcha input is correct

I am trying to implement Google's reCaptcha on my website's query form in php.
When the CAPTCHA is entered incorrectly,
I get: "No. CAPTCHA is not entered correctly".
But when the CAPTCHA is entered correctly,
What I expect: "Everything looks good" OR "CAPTCHA is correct but other values are incorrect."
What I get: Blank Page
Here is how I am implementing it:
$var1 = $_POST["var1"];
$var2 = $_POST["var2"];
require_once('recaptchalib.php');
$privatekey = "<private key I got from reCaptcha>";
$respCaptcha = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if(!$respCaptcha->is_valid) {
echo "No. CAPTCHA is not entered correctly";
} elseif( is_numeric($var1)
&& preg_match("/^[a-zA-Z ]*$/",$var2)) {
echo "Everything looks good";
} else {
echo "CAPTCHA is correct but other values are incorrect.";
}
Please help. What am I doing wrong here?
Try putting the if just inside the else...
Also is this directly pasted because the line
'Do something...' is not commented

Why do I get "Undefined index: recaptcha_response_field"?

This is my form code:
echo '
<div class="ctext" id="form">
<table><form method="post" action="prashanja.php">
<tr><td>Name:</td><td><input type="text" name="name"/></td></tr>
<tr><td>Question:</td><td><textarea cols="55" rows="4" name="prashanje"> </textarea></td></tr>
<tr><td></td><td >' . recaptcha_get_html($publickey) . '<button name="btn" value="submit">Enter</button></td></tr>
</form></table>
</div>';
And this is the validation code:
if (isset($_POST['btn'])) {
$name = mysql_real_escape_string(strip_tags($_POST['name']));
$prashanje = mysql_real_escape_string(strip_tags($_POST['prashanje']));
$date = time();
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
# was there a reCAPTCHA response?
if ($_POST["recaptcha_response_field"]) {
$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
$query = "INSERT INTO prashanja VALUES(NULL,'$name','$prashanje','','$date')";
if (!mysql_query($query))
echo 's... happens';
else
echo 'cool';
} else {
# set the error code so that we can display it
$error = $resp->error;
}
}
}
These segments are on the same page. The first segment generates the form and the second segment checks if the correct button is pressed, initializes the variables and than checks the recaptcha and does some operations. I get the error "Undefined index: recaptcha_response_field". I suspect that recaptcha won't work if the validation is done on the same page as the form that sent the variables. Are my suspicions correct and is it better if I move the validation code to another file?
It works if you validate on the same page.
You are accessing the variable for checking:
if ($_POST["recaptcha_response_field"]) {
That most likely causes the error, it should be
if (isset($_POST["recaptcha_response_field"])) {
Move the
form
tag outside of the
table
tag. I don't know why but it worked.

How to Verify reCAPTCHA

I'm trying to implement reCAPTCHA on a contact form & I'm stuck. The function is showing up on my page (that was easy) but now I'm confused as to how to verify the CAPTCHA. I have a "send-mail.php" file that verify's the input data & then sends it to a specified email address. Should I somehow include the reCAPTCHA verification within this file? If so, how do I do so?
Link to contact form
Your help is much appreciated.
Assuming you're using the "official" PHP plugin (http://recaptcha.net/plugins/php/), this is what I used to use:
<?php
# Get a key from http://recaptcha.net/api/getkey
$publickey = "";
$privatekey = "";
#require_once( 'path/to/recaptchalib.php' );
# the response from reCAPTCHA
$resp = null;
# was there a reCAPTCHA response?
if( $_POST["recaptcha_response_field"] )
{
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if( $resp->is_valid )
{
}
else {
if( $resp->error == 'incorrect-captcha-sol') {
}
}
}
?>
This is pretty much copy-pasta from the source docs, if I recall - to be honest, I switched to Akismet a while ago...

Change reCAPTCHA response to alert() instead of loading new page

I'm using reCAPTCHA and I'd like to show the response if the CATPCHA was filled in incorrectly as an alert(); rather than it loading a new page. How can I do that?
This is the form action:
<form id="form" method="POST" action="verify.php">
With this in the verify.php file:
<?php
require_once('recaptchalib.php');
$privatekey = "(my key)";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
Call your method on form submit like--
<form id="form" method="POST" action="verify.php" onsubmit="mymethod()" >
OR use jquery, something like that --
<script>
$("form").submit(function() {
if ($("input:first").val() == "correct") {
$("span").text("Validated...").show();
return true;
}
$("span").text("Not valid!").show().fadeOut(1000);
return false;
});
</script>
You can write the Javascript for the alert on the new page.
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
echo("<script>alert('The reCAPTCHA wasn't entered correctly. Go back and try it again.');</script">);
die();
} else {
// Your code here to handle a successful verification
}

about recaptcha validation

Im working with a php form validation, which involves reCAPTCHA as well.
Once the form is submitted, I validate the form fields, and store the error messages like
if( !$this-> valid_username($username) ){
$this->error = "username is invalid <br />";
}
and similarly other fields.
Now, How can I access the $response->is_valid in my validation class so that I can display the captcha error sth like
if( !$response->is_valid ){
$this->error .= "Invalid captcha. <br />";
}
The idea is to display all fields errors at once.
I hope my question is clear, I'd appriciate any help.
I'm not exacly sure what you mean but I guess you need this:
http://code.google.com/intl/nl-NL/apis/recaptcha/docs/php.html
first include the usual reCAPTCHA library like this:
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
Then you can check the result like you mentioned above:
if (!$resp->is_valid) {
$this->error .= "Invalid captcha. <br />";
}

Categories