Jump to content

Validate The Form Using Recaptcha, On Success Post Data To New Form


yesvee

Recommended Posts

Hi

I'm new to php, i've been struggling for 2 days to write a simple code with recaptcha. My requirements is

i have a registration form (reg.php) with 20 fields on it after all the required information is filled in the form data is submitted to Create Account page (CreateAccount.php).

 

I placed a recaptcha control on the first page (reg.php) to secure the form from bots. I'm doing some javascript validation on the client side for required fields, and finally validating if correct recaptcha code is entered on the server side. That means the form is submitted to itself first for recaptcha validation, after the code is valid i want to resubmit the data to Account Creation page. i don't know how to do this. the CreateAccount.php has got a complex logic so i can not move it to the same reg.php page.

 

i tried the following but with no luck...

 

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) {

//redirect to AccountCreation.php

header("Location: AccountCreation.php");

 

} else {

$error = $resp->error;

}

}

 

please help me how this is done is php. Is there any other way of doing it simply...

Link to comment
Share on other sites

The reCaptcha is made the way it is for a reason. What you are doing should work. If you mean you want the data from the form, to be submitted to the page after the header, you need to add variables to the URL

 

Header("Location: http://mydomain.com/AccountCreation.php?user=blah&name=blahblah&yea=totally");
die;

Edited by SocialCloud
Link to comment
Share on other sites

Thanks for the reply,

i tried the above code but it is not working, it stays on the same page reg.php after successful validation.

 

Also on sending variables is it possible to use post method as i have 20 fields and some of them are text area fields.

Link to comment
Share on other sites

If you header, it is not possible. Try using a function, that way you can stay on the same page, and use the code in your AccountCreation.php

 


// Get a key from https://www.google.com/recaptcha/admin/create
$publickey = "xxxxxxxxxxxxxx";
$privatekey = "xxxxxxxxxxxxx";

# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;

# was there a reCAPTCHA response?
if(isset($_POST["recaptcha_response_field"])) {
       $resp = recaptcha_check_answer ($privatekey,
                                       $_SERVER["REMOTE_ADDR"],
                                       $_POST["recaptcha_challenge_field"],
                                       $_POST["recaptcha_response_field"]);

       if ($resp->is_valid) {
// if by function
               RegisterUser(Your 20 variables each separated by a comma);
       } else {
               # set the error code so that we can display it
               $error = $resp->error;
echo "Error captcha incorrect";
       }
}

Link to comment
Share on other sites

Hi

As i said the accountcreation.php has got very complex code and moving that into a function in the reg.php is inviting more troubles i believe.

Can i store the data from all the 20 fields in a session and use them in accountcreation.php? but even for that i should be redirected to the second page, which is not working for me using the header(), is there any other way of doing it, like auto submit etc.. please think if you have any suggestions, It is bugging me for almost 2 days..

Link to comment
Share on other sites

as SocialCloud said, AccountCreation.php shouldn't have to be complex, after all you said it is dealing with $_POST variables so surely it is only checking them for empties, maybe a few text patterns, and depending on your site content and what the users profile is maybe even directory creations? these should all be able to be put into a function without any problems. the only changes you would have to make is how it outputs any messages from validation.

Link to comment
Share on other sites

I think i should go by your idea of moving the AccountCreation code into reg.php, as i found a file field for file attachements by the user.

so i have a feeling that transfering all the data along with the file attachemnts is beyond by calibre now :(

so i'll try to move the code back to the first page...it is a tough task but doable.

php is not easy as many people say :(

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.