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 />";
}
Related
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"]);
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
I have looked for similar questions and I did not find my particular case.
I am using the PHP Captcha plugin within a form that I have. I handle the incorrect and correct captcha entries very similar. If the user's phrase is correct I throw a javascript "success" alert, send the form email, and then send them back to the last page. If incorrect I throw a javascript "your incorrect" alert and send them back to the last page.
My problem- if they are incorrect I need to refresh Captcha because with an incorrect Captcha entry you will always need to refresh the image for another attempt. Also if they are correct I want the field cleared but only cleared when correct, if incorrect I want to keep the forms data. How can I do this?
Here is my PHP captcha code so you can see my attempt. Let me know if you want the html... (also I checked all entries with JS before POST)
<?php
/*set email*/
$myemail = "email#email.com";
/* Check all form inputs using check_input function */
$name = $_POST['name'];
$companyName = $_POST['companyName'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$TimeForContact = $_POST['TimeForContact'];
$aval = $_POST['aval'];
$messageFromFrom = $_POST['message'];
$firstTime = $_POST['firstTime'];
$subject = "Some Subject";
require_once('recaptcha-php-1.11/recaptchalib.php');
$privatekey = "*someprivatekey*"; //I took this out for the question
$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
echo '<script type="text/javascript"> alert ("You have inserted the wrong phrase in the Captcha box. Please try again! Thank you."); window.history.back();</script>';
exit();
} else {
$message = "some message
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
echo '<script type="text/javascript"> alert ("Success! You have emailed your submission. Please await our response. Thank you."); window.history.back()</script>';
exit();
}
?>
I originally tried reloading the page through JS, like:
window.reload(history.back());
or
window.location.reload(history.go(-1));
no success with either + multiple combinations similar to that.
So to reiterate the question:
How can I refresh form/captcha when desired
OR
What is your practiced behavior for submitting a captcha form?
Thank you.
You should be doing something like this. Essentially checking for errors and pushing errors into an error array that you can later loop over and display to the user. Upon submit, you check the recaptcha and if its invalid, it will automatically clear the field. Do not use alerts or exit() or anything of that sort, a simple $errors array should suffice.
//untested code
<?php
//check to make sure they submitted the form
if(isset($_POST['submit'])){
$errors = array();
$myemail = "email#email.com";
$name = $_POST['name'];
$companyName = $_POST['companyName'];
/* ... */
//recaptcha check
require_once('recaptcha-php-1.11/recaptchalib.php');
$privatekey = "*someprivatekey*";
$resp = recaptcha_check_answer (
$privatekey,$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]
);
//validate all data here
if (!$resp->is_valid) {
array_push($errors, "recaptcha invalid please try again");
}else if( /* name is invalid */) {
array_push($errors, "name invalid");
}else if (){
/* keep validing ...... with else if*/
}
/*....*/
else{
//everything validated so were good
mail($myemail, $subject, $message);
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="process.php">
<!-- Your form here, "process.php" is this page itself -->
<input name="submit" type="submit" value="Send">
</form>
</body>
</html>
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...
I'm trying to get reCaptcha working with a form on my website and for some reason I keep getting an error that the wrong captcha was entered. Does anyone see anything wrong with my code?
require_once('includes/recaptchalib.php');
$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
if (isset($category)) {
if ($edit == 'edit') {
include "includes/updatelisting.php";
} else {
$response = recaptcha_check_answer($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($response->is_valid) {
include "includes/insertlisting.php";
} else {
echo "Eh, That wasn't right. Try Again.";
}
}
} else {
Here is the code in the actual form..
// Display the reCaptcha form
echo recaptcha_get_html($publickey, $error);
I found the issue. Apparently my tags were inside the tags. Once I put the form tags outside the table tags everything worked perfectly. Very strange. Here is a link to the answer I found Need help with reCAPTCHA - keep getting incorrect-captcha-sol
When you call echo recaptcha_get_html($publickey, $error); did you already have $error declared?
You might want to declare it first (and yes, make it a string with one space):
$error = ' ';
echo recaptcha_get_html($publickey, $error);
Also, check to see if $category really is set before trying to validate.