News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Adding reCaptcha to Post Submission Page?

Started by Anarchology, October 06, 2009, 11:50:37 PM

Previous topic - Next topic

Anarchology

EDIT: This has now been SOLVED thanks to the maker of the reCaptcha Mod! I was pointed to this MOD below

http://custom.simplemachines.org/mods/index.php?mod=734


Here's the deal. I have one board on my forum that allows guests to post. The obvious problem is that it gets spammed by what I would figure are just regular spam bots.

The solution I'm working on right now is that anyone trying to post in that section must input a reCaptcha that I hopefully would like to center above the "Submit/Preview/Spell Check" buttons on the post screen.

Also, I plan on making it just for guests, so it would be hidden to logged in members. I've got the code below, but not sure exactly where to put it either in "Themes/default/post.template.php" or "Sources/Post.php".

This is the code...
if ($context['user']['is_guest'])
echo '

require_once('recaptchalib.php');
$publickey = "my-recaptcha-key"; // you got this from the signup page
echo recaptcha_get_html($publickey);


My current attempts just shoot up as a Parse Error. Any ideas guys?

Thanks in Avdance!

JBlaze

For mod Related Questions it is best to ask the mod author. You can find a link to a topic for the mod or a support board on the mod's page in the Mod Site
Jason Clemons
Former Team Member 2009 - 2012

Anarchology

Quote from: JBlaze on October 06, 2009, 11:51:34 PM
For mod Related Questions it is best to ask the mod author. You can find a link to a topic for the mod or a support board on the mod's page in the Mod Site

Actually, I'm not asking about any current mod because there isn't one. I am actually referring to the use of reCaptcha (as in from recaptcha.net) within the post submission page. The only mod close to what I'm looking to achieve ads reCaptcha to the Registration page.

I hope that helps out a little bit.

Arantor

For that part:
if ($context['user']['is_guest']) {
require_once('recaptchalib.php');
$publickey = "my-recaptcha-key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
}

miranon

I also tried to enable reCaptcha for guest posting, but with no success. So i would really appreciate help from experienced coders.  ;)

Arantor

Well, I know nothing of the reCaptcha mod, you could try asking its author if he plans to enable this (as it sounds like it's only on registration)

Anarchology

Alright, this is what I have currently tried. I'm getting close to cracking this, but still running into problems. I will now explain in greater detail below. Let us put a couple minds together and get this working. I don't make any Mods, so if this is somehow perfected, anyone is more than welcome to take the idea to make a Mod for all SMF people! 8)

First off, I'm going off of the information provided at http://recaptcha.net to integrate the working reCaptcha for posts. I signed up (for free obviously) and added my website in order to acquire my own Public and Private Keys needed to be put in the code for proper verification to allow reCaptcha to work.

The instructions in order to add reCaptcha to PHP are below straight from their site:
http://recaptcha.net/plugins/php/

Quote1. Download the reCAPTCHA Library, unzip it, and copy recaptchalib.php to the directory where your forms live.
2. If you haven't done so, sign up for an API key.
3. Now we're ready to start modifying your code. First, we'll add code to display the CAPTCHA:
require_once('recaptchalib.php');
$publickey = "..."; // you got this from the signup page
echo recaptcha_get_html($publickey);


4. In the code that processes the form submission, you need to add code to validate the CAPTCHA. Otherwise, the CAPTCHA will appear, but the answers won't be checked. The validation code looks like:

require_once('recaptchalib.php');
$privatekey = "...";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}
(Go to the site listed in order to see the full explanation)

I used Arantor's rendition of the code above in order to properly place it into ./Themes/default/Post.template.php as shown below with a small screen shot. You can post the code where you see fit on the posting screen.

ALSO, this was setup just for guests to be seen. Otherwise, reCaptcha is not seen by regular posting members. I'm just trying to utilize this great tool to kill off spam bots!

Themes/default/Post.template.php

find
// Spell check button if the option is enabled.
if ($context['show_spellchecking'])
echo '
<input type="button" value="', $txt['spell_check'], '" tabindex="', $context['tabindex']++, '" onclick="spellCheck(\'postmodify\', \'message\');" />';


add after
//Recaptcha on Guest Posts
      if ($context['user']['is_guest']) {
         require_once('recaptchalib.php');
         $publickey = "your-recaptcha-public-key"; // you got this from the signup page
         echo recaptcha_get_html($publickey);
}


And the result is what you see in the picture below (not the default template I'm using). It will show up below the POST/PREVIEW/SPELL CHECK buttons. Placement can be anywhere you like!
The problem with what is listed above is that reCaptcha DOES NOT WORK! It still allows guests to post without inputting anything in the reCaptcha.

So, my next attempt was to mess with the second part of code off of the reCaptcha PHP page that says is needed in order to make reCaptcha work.

First I tried adding it to Themes/default/Post.template.php where the initiative code is housed. Either I cannot get the code properly setup (help needed please), or it does not belong there.

I then added it to the bottom of Sources/Post.php as shown below. This might be completely incorrect, so any incite would be helpful!

find
?>

add before
require_once('recaptchalib.php');
$privatekey = "my-private-recaptcha-key";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}


No dice. I get a white screen with just this text.
QuoteThe reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)

I have also been playing around with exactly where I am to put the "recaptchalib.php" file included within the download from the reCaptcha site. I've put it in the ROOT directory, Sources/, Themes/default/, and Themes/mycustomtheme/.

I still don't know if the recaptchalib.php needs to be modified. This has been throwing me for a loop! Incite is more than welcome!

Thanks in Advance!

Arantor

No, it wouldn't be the end of Post.php. You'd be adding it in the Post2() function where the rest of the checking takes place.

You'd have it divert to reCaptcha there, and drop the error into the error array along with other errors as Post2() executes.

Anarchology

Quote from: Arantor on October 08, 2009, 10:16:46 AM
No, it wouldn't be the end of Post.php. You'd be adding it in the Post2() function where the rest of the checking takes place.

You'd have it divert to reCaptcha there, and drop the error into the error array along with other errors as Post2() executes.

Forgive me. I'm quite confused with what you mean. Can you please elaborate. I figure what you mean is that the second part of code does not belong in Posts.php, but rather along with the first within Post.template.php. If that is the case, I'm having problems parsing the code properly.

If and when you might have the time, can you please help me out with this? Also, other people looking for this, playing around with it would help out. I still think this would be one valuable Mod for SMF forums.

Arantor

Part of the code has to live in Post.php, where the 'post' button gets pressed. This is where the code lives to receive the post you've made, and where - I think - the confirmation of being valid gets sent.

I really don't know enough about reCaptcha, I'd personally be posting this in the thread for the original reCaptcha mod, about making it an extension to cover posting too, since the author of that mod already did the bulk of the work about making reCaptcha work with SMF.


Advertisement: