0

I am trying to add recaptcha into my custom PHP form and am confused about how to do so. Recaptcha requires adding recaptchalib.php into the form, but if I add verify.php then my form will not process, as I am using my PHP file to process my form.

<form method="POST" action="process.php" id="form-ok"> 

The documentation is a bit confusing. My question is, what do I need to do to process two actions?

Any idea how can I get this working?

Process.php

<?php $redirectTo = '/thankyou.html'; $subject = 'New message from site'; // Email SUBJECT field $receive = array( '[email protected]' ); if($_POST['email_check'] == '') { if (isset($_POST['first_name'])){ $message = '<table width="100%" border="0" cellspacing="0" cellpadding="8" style="border:1px solid #f3f3f3"> <tr> <td colspan="3" height="30" style="font-size:20px"><strong>' . $subject . '</strong></td> </tr> <tr> <td width="100" bgcolor="#f3f3f3"><strong>First Name: </strong></td> <td width="14" height="30" bgcolor="#f3f3f3">&nbsp;</td> <td width="305" bgcolor="#f3f3f3">' . $_POST ['first_name'] . '</td> </tr> <tr> <td><strong>Last Name: </strong></td> <td width="14" height="30">&nbsp;</td> <td>' . $_POST ['last_name'] . '</td> </tr> <tr> <td bgcolor="#f3f3f3"><strong>Email: </strong></td> <td bgcolor="#f3f3f3" width="14" height="30">&nbsp;</td> <td bgcolor="#f3f3f3">' . $_POST ['email'] . '</td> </tr> <tr> <td><strong>Phone Number: </strong></td> <td width="14" height="30">&nbsp;</td> <td>' . $_POST ['phone'] . '</td> </tr> <tr> <td bgcolor="#f3f3f3"><strong>Check: </strong></td> <td bgcolor="#f3f3f3" width="14" height="30">&nbsp;</td> <td bgcolor="#f3f3f3">'; foreach($_POST['role'] as $value) { $message.=$value.'<br>'; } $message.='</td> </tr> <tr> <td><strong>Message: </strong></td> <td width="14" height="30">&nbsp;</td> <td>' . $_POST ['message'] . '</td> </tr> <tr> <td><strong>Referer:</strong></td> <td width="14" height="30">&nbsp;</td> <td>' . $_SERVER ['HTTP_REFERER'] . '</td> </tr> <tr> </table>'; for ($i = 0; $i < count($receive); $i++){ $to = $receive[$i]; $headers = 'MIME-Version: 1.0' . "\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n"; $headers .= 'From: ' . $_POST['email'] . "\n" . 'Reply-To: ' . $_POST['email'] . "\n"; mail($to, $subject, $message,$headers); } header('Location: '.$redirectTo); } } else{ header('Location:'.$_SERVER['HTTP_REFERER']); die(); } ?> 
5
  • 1
    what so confusing about it? its clear as crystal clear.. add recaptchalib.php both in your client and server. it doesnt matter you name it verify.php or in your case process.php Commented Feb 25, 2015 at 2:24
  • So i need to add my process code in verify.php right? Commented Feb 25, 2015 at 2:27
  • the good news is,,,,,,,,you dont need verify.php at all!!!viola!! Commented Feb 25, 2015 at 2:27
  • means? then how this will validate recaptcha? can you please answer below? Commented Feb 25, 2015 at 2:30
  • You need to include the ReCAPTCHA validation code in your process.php, and run it as part of your default form processing. It's a single action, since the ReCAPTCHA information is submitted as part of the form. Commented Feb 25, 2015 at 3:02

2 Answers 2

3

Add your recaptchalib.php in your dir.

YOUR PROCESS.PHP:

require_once "../recaptchalib.php"; // where you store recaptchalib.php $secret = "6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx"; //your secret key $resp = null; $error = null; $reCaptcha = new ReCaptcha($secret); if ($_POST["g-recaptcha-response"]) { $resp = $reCaptcha->verifyResponse($_SERVER["REMOTE_ADDR"], $_POST["g-recaptcha-response"]); } if (isset($_POST['cmdlogin'])){ if ($resp != null && $resp->success) { echo "<script>alert('Success Verifying Recaptcha!');</script>"; echo "<meta http-equiv='refresh' content='0; url=login.php'>"; exit(); } <form method="post" action="process.php"> .....other codes--- <div class="g-recaptcha" data- sitekey="6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx"> </div> .....other codes--- </form> 

Full Tutorial, check here: https://github.com/google/ReCAPTCHA/tree/master/php

Sign up to request clarification or add additional context in comments.

6 Comments

This is version 1.0 or 2.0?
STEP-4: VALIDATION OF USER RESPONSE ---> Where should i place this?
its going through! no validation!
see the link for complete tutorial, or, click here> github.com/google/ReCAPTCHA/tree/master/php
Well, It is working with 1.0 but not with 2.0 so i think there is some problem with implementation
|
1

Client Side (How to make the CAPTCHA image show up)

 <form method="post" action="process.php"> <?php require_once('recaptchalib.php'); $publickey = "YOUR_PUBLIC_KEY"; // you got this from the signup page echo recaptcha_get_html($publickey); ?> <input type="submit" /> </form><br> <!-- more of your HTML content --> 

Server Side The following code should be placed at the top of the process.php file:

<?php require_once('recaptchalib.php'); $privatekey = "YOUR_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 { // Your code here to handle a successful verification } ?> 

7 Comments

why do you need verifiy.php? id dont recall any verify.php in my answer
Yep now working fine! Huge thanks! One question, It shows incorrect captcha message on next page "after reloading" how can i make it to validate it live? a simple one like this: google.com/recaptcha/api2/demo
what version are you using @RandykaYudhistira? it's NOT USable
@don i dont use it. Kingo Ping use that. i just answer his question
@KingoPing you need AJAX for that. its different case then
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.