Jump to content


Photo

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

php recaptcha

  • Please log in to reply
11 replies to this topic

#1 yesvee

yesvee

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 30 November 2012 - 06:31 AM

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...

#2 SocialCloud

SocialCloud

    Advanced Member

  • Members
  • PipPipPip
  • 619 posts

Posted 30 November 2012 - 08:20 AM

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, 30 November 2012 - 08:22 AM.


#3 yesvee

yesvee

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 30 November 2012 - 08:42 AM

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.

#4 SocialCloud

SocialCloud

    Advanced Member

  • Members
  • PipPipPip
  • 619 posts

Posted 30 November 2012 - 08:47 AM

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";
        }
}


#5 yesvee

yesvee

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 30 November 2012 - 08:55 AM

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..

#6 SocialCloud

SocialCloud

    Advanced Member

  • Members
  • PipPipPip
  • 619 posts

Posted 30 November 2012 - 08:58 AM

Creating the SESSION is a bad idea because you're basically logging the user in without even validating them (unless you unset all variables at the end). Please post AccountCreation.php I'm sure it is not that complex.

#7 White_Lily

White_Lily

    Advanced Member

  • Members
  • PipPipPip
  • 523 posts

Posted 30 November 2012 - 09:08 AM

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.

"In order to be irreplaceable, one needs to be different from another."


#8 yesvee

yesvee

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 30 November 2012 - 09:08 AM

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 :(

#9 yesvee

yesvee

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 30 November 2012 - 09:10 AM

or shall use javascript to validate a captcha field, that seems much simpler

#10 SocialCloud

SocialCloud

    Advanced Member

  • Members
  • PipPipPip
  • 619 posts

Posted 30 November 2012 - 09:12 AM

It is not easy starting off if you have little experience in other languages. All you need is practice

#11 White_Lily

White_Lily

    Advanced Member

  • Members
  • PipPipPip
  • 523 posts

Posted 30 November 2012 - 09:14 AM

Actually Javascript isn't easier as you would have to make sure the javascript works accross all major browsers. Especially IE7 as it seems that one is most problematic.

"In order to be irreplaceable, one needs to be different from another."


#12 yesvee

yesvee

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 30 November 2012 - 09:26 AM

Thanks very much for your help, yes i need more practice...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com