yesvee Posted November 30, 2012 Share Posted November 30, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/ Share on other sites More sharing options...
MDCode Posted November 30, 2012 Share Posted November 30, 2012 (edited) 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 November 30, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396432 Share on other sites More sharing options...
yesvee Posted November 30, 2012 Author Share Posted November 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396440 Share on other sites More sharing options...
MDCode Posted November 30, 2012 Share Posted November 30, 2012 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396444 Share on other sites More sharing options...
yesvee Posted November 30, 2012 Author Share Posted November 30, 2012 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.. Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396449 Share on other sites More sharing options...
MDCode Posted November 30, 2012 Share Posted November 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396450 Share on other sites More sharing options...
White_Lily Posted November 30, 2012 Share Posted November 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396454 Share on other sites More sharing options...
yesvee Posted November 30, 2012 Author Share Posted November 30, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396455 Share on other sites More sharing options...
yesvee Posted November 30, 2012 Author Share Posted November 30, 2012 or shall use javascript to validate a captcha field, that seems much simpler Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396456 Share on other sites More sharing options...
MDCode Posted November 30, 2012 Share Posted November 30, 2012 It is not easy starting off if you have little experience in other languages. All you need is practice Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396458 Share on other sites More sharing options...
White_Lily Posted November 30, 2012 Share Posted November 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396459 Share on other sites More sharing options...
yesvee Posted November 30, 2012 Author Share Posted November 30, 2012 Thanks very much for your help, yes i need more practice... Quote Link to comment https://forums.phpfreaks.com/topic/271398-validate-the-form-using-recaptcha-on-success-post-data-to-new-form/#findComment-1396461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.