Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 4
How do I integrate CAPTCHA? 
 
RPGShop
Sr. Member
 
Total Posts:  143
Joined:  2008-06-16
 

How would I go about integrating http://recaptcha.net/ into the review and contact forums? This requires me to drop some code in the form (easy enough I think it’s in app/design/frontend/default/default/template/review form.phtml ) but then where does it get processed as that’s where the compare call with the private key has to be checked to see if it’s valid. It posts to “review/product/post/id/4961/” but that’s someone confusing.

I’m getting 5 or 6 spamer bots using these forms and it’s very annoying.

Example of it’s use on one of my older sites: http://www.RPGOverstock.com/contact.php

James
http://www.rpgshop.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  44
Joined:  2008-04-05
 

Hi,

I have tried to integrate recaptcha and I was partially successfully. I have tried for customer registration page only. But this can be applied to other forms as well. This is for magento 1.1.6 version only. Here is the link where to download the recapthcha file:
http://recaptcha.net/plugins/php/
Here is the step:
Step-1
Copy the recaptchalib.php file in to /app/code/core/Mage/Customer/controllers directory and app/design/frontend/default/default/template/customer/form directory.

Step-2:
Open app/design/frontend/default/yourstore/template/customer/form/register.html .

Go to line 57 and add the following line:
<?php
require_once('recaptchalib.php');
$publickey = "xxxxxxx "; // you got this from the signup page

?>
<li>
<div class="input-box">
<label for="captcha_field"><?php echo recaptcha_get_html($publickey) ?> <span class="required">*</span></label><br/>

</div>
</li>

Step-3
Open app/code/core/Mage/Customer/controllers/AccountController.php file and go to line number 230
replcae the following lines:

$validationResult = $customer->validate();
if (true === $validationResult ) {

by

require_once(’recaptchalib.php’);
$privatekey = “xxxx”; // This is the private key you get from recpatcha registration
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);

$validationResult = $customer->validate();
if (true === $validationResult && $resp->is_valid) {
Step-4
Then cache in admin and that is it.

Problem:
The problem is two things, I hope some one can fix it very easily. I am not proficient in php programming. some expert can fix it. Please help .
1) First problem is when you submit the page with entering inside captcha field correctly, for some reason I am getting Can’t save customer error message and the page is not redirected to customer account page. But if you check in admin, the customer is added to db.
2) Second problem is copying the recaptchalib.php file twice to both directory. It would be nice to copy that recaptcha library in only one directory and then use require_once(’recaptchalib.php’); with suitable directory prefix.

Please help if any one know about it.

Thanks
Brian

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  44
Joined:  2008-04-05
 

Can anyone from magento team help to fix this issue?

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  44
Joined:  2008-04-05
 

it is working now.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jeff.d
Sr. Member
 
Total Posts:  103
Joined:  2008-05-26
 

Did you have to do anything to fix it or did it just start working on its’ own? I might try this out.

 
Magento Community Magento Community
Magento Community
Magento Community
 
RPGShop
Sr. Member
 
Total Posts:  143
Joined:  2008-06-16
 

Blah, I just looked at my customer reviews… 1 of 25 pages to review… spam spam spam… grrr…

I’m going to have to give this another shot. Thanks for the info.

James

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  44
Joined:  2008-04-05
 

Hi,

I got it working and fixed all the problems. I also implemented this in contact form successfully. I will write all the steps in this forum very soon.

Thanks
Brian

 
Magento Community Magento Community
Magento Community
Magento Community
 
Nils
Jr. Member
 
Total Posts:  10
Joined:  2008-09-18
 

I look forward to your post!

Thanks Nils

 
Magento Community Magento Community
Magento Community
Magento Community
 
bonhommie
Member
 
Total Posts:  60
Joined:  2008-06-02
 

yes BrianPat,
please post very soon!!!!! I am so frustrated with these bots.

 
Magento Community Magento Community
Magento Community
Magento Community
 
brendanb
Mentor
 
Total Posts:  1093
Joined:  2008-07-16
London, United Kingdom
 

hey brian,

would be nice to see what ur ended up with here on this.

cheers
Brendan

 
Magento Community Magento Community
Magento Community
Magento Community
 
Naresh Vajawat
Member
 
Total Posts:  47
Joined:  2008-09-22
India
 

Hi BrianPT ,

Did you get a chance to post the code for recaptcha on the forum. I apprecaite your help.

Thanks,
Naresh

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  44
Joined:  2008-04-05
 

Hi All,

Sorry for late reply. I was not feeling well and as a result, I could not reply to all the comments above.

I am doing this for my multi store. If you do not have multi store, you can only use for default storeid=1 and skip validation of all other dtores
Here is the step: for 1.1.6 magento

This recaptcha integration is for Contact us,send friends and customer registration.
1) Download recaptcha from this site: http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest

2) Unzip the above file in temp directory

3) Create a directory under lib directory and name it as recaptcha

4) Copy the recaptchalib.php file in to lib/recaptcha/ directory

5)Files that need to be changed are as follows:
For appearing in UI
a)app/design/frontend/default/default/template/contacts/form.phtml
b)app/design/frontend/default/default/template/customer/form/register.phtml
c)app/design/frontend/default/default/template/sendfriend/send.phtml

For validation processing, you have to change magento core files. I personally do not like to change the core files, but as I do not know how to avoid this, I changed this to get it working.
d)app/code/core/Mage/Contacts/controllers/IndexController.php
e)app/code/core/Mage/Customer/controllers/AccountController.php
f)app/code/core/Mage/Sendfriend/controllers/ProductController.php

6) Here is the code change for 5a,5b and 5c in above
Please copy the following code
<?php

require_once 'lib/recaptcha/recaptchalib.php';

$storeId = Mage::app()->getStore()->getId();


if($storeId == 1)
{

$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxx"; // you got this from the signup page

}

// The following code is only for multistore, - START

if($storeId == 6) //Chnage this number according to your own store id
{

$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxx"; // you got this from the signup page

}
if($storeId == 7) //Chnage this number according to your own store id
{

$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxx"; // you got this from the signup page

}

// The above code is only for multistore, - END

?>

in line number 31 in form.phtml,line number 57 in register.phtml and line number 34 in send.phtml.

7) Here is the code change for 5d,5e and 5f
Please replace the following code in line 64 of indexcontroller.php

$post = $this->getRequest()->getPost();
if ( $post ) {
$translate = Mage::getSingleton(’core/translate’);
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
$postObject = new Varien_Object();
$postObject->setData($post);

$mailTemplate = Mage::getModel(’core/email_template’);
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array(’area’ => ‘frontend’))
->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
null,
array(’data’ => $postObject)
);

if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}

$translate->setTranslateInline(true);

Mage::getSingleton(’customer/session’)->addSuccess(Mage::helper(’contacts’)->__(’Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.’));
$this->_redirect(’*/*/’);

return;
} catch (Exception $e) {
$translate->setTranslateInline(true);

Mage::getSingleton(’customer/session’)->addError(Mage::helper(’contacts’)->__(’Unable to submit your request. Please, try again later’));
$this->_redirect(’*/*/’);
return;
}

} else {
$this->_redirect(’*/*/’);
}
}

}

by the following

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  44
Joined:  2008-04-05
 

require_once ‘lib/recaptcha/recaptchalib.php’;

$storeId = Mage::app()->getStore()->getId();

if($storeId == 1)
{
$privatekey = “xxxxxxx”; // you got this from the signup page

}

// The following code is only for multistore, - START
if($storeId == 5) //Chnage this number according to your own store id

{

$privatekey = “xxxxxxxx”; // you got this from the signup page

}
if($storeId == 6) //Chnage this number according to your own store id
{

$privatekey = “xxxxxxxx”; // you got this from the signup page

}
if($storeId == 7) //Chnage this number according to your own store id
{

$privatekey = “xxxxxxxxxxxxxxxxxxxx”; // you got this from the signup page

}

// The above code is only for multistore, - END

$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
$post = $this->getRequest()->getPost();
if ( $post) {
if( $resp->is_valid ){
$translate = Mage::getSingleton(’core/translate’);
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
$postObject = new Varien_Object();
$postObject->setData($post);

$mailTemplate = Mage::getModel(’core/email_template’);
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array(’area’ => ‘frontend’))
->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
null,
array(’data’ => $postObject)
);

if (!$mailTemplate->getSentSuccess() ) {
throw new Exception();
}
$translate->setTranslateInline(true);

Mage::getSingleton(’customer/session’)->addSuccess(Mage::helper(’contacts’)->__(’Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.’));
$this->_redirect(’*/*/’);

return;
} catch (Exception $e) {
$translate->setTranslateInline(true);

Mage::getSingleton(’customer/session’)->addError(Mage::helper(’contacts’)->__(’Unable to submit your request. Please, try again later’));
$this->_redirect(’*/*/’);
return;
}
}
else
{
Mage::getSingleton(’customer/session’)->addError(Mage::helper(’contacts’)->__(’Please enter the word correctly for human verfication. Unable to submit your request.’));
$this->_redirect(’*/*/’);
return;
}

} else {
$this->_redirect(’*/*/’);
}
}

}

For code change in 5e(/AccountController.php ) file, replace the following:line number 226 after try { block

try {
$validationResult = $customer->validate();
if (true === $validationResult) {

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  44
Joined:  2008-04-05
 

by the following:
************************************************************************************************************************************************
try {

require_once ‘lib/recaptcha/recaptchalib.php’;

$storeId = Mage::app()->getStore()->getId();

if($storeId == 1)
{
$privatekey = “xxxxxxx”; // you got this from the signup page

}

// The following code is only for multistore, - START
if($storeId == 5) //Chnage this number according to your own store id

{

$privatekey = “xxxxxxxx”; // you got this from the signup page

}
if($storeId == 6) //Chnage this number according to your own store id
{

$privatekey = “xxxxxxxx”; // you got this from the signup page

}
if($storeId == 7) //Chnage this number according to your own store id
{

$privatekey = “xxxxxxxxxxxxxxxxxxxx”; // you got this from the signup page

}

// The above code is only for multistore, - END

$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);

$validationResult = $customer->validate();

if (true === $validationResult && $resp->is_valid) {
************************************************************************************************************************************************

For code change in 5f ProductController.php line number 117
replace the following lines
try {
$validateRes = $sendToFriendModel->validate();
if (true === $validateRes) {

by the following lines:

try {
require_once ‘lib/recaptcha/recaptchalib.php’;

$storeId = Mage::app()->getStore()->getId();

if($storeId == 1)
{
$privatekey = “xxxxxxx”; // you got this from the signup page

}

// The following code is only for multistore, - START
if($storeId == 5) //Chnage this number according to your own store id

{

$privatekey = “xxxxxxxx”; // you got this from the signup page

}
if($storeId == 6) //Chnage this number according to your own store id
{

$privatekey = “xxxxxxxx”; // you got this from the signup page

}
if($storeId == 7) //Chnage this number according to your own store id
{

$privatekey = “xxxxxxxxxxxxxxxxxxxx”; // you got this from the signup page

}
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
$validateRes = $sendToFriendModel->validate();
if (true === $validateRes && $resp->is_valid) {
************************************************************************************************************************************************
Please let me know if that helps

Thanks and Kind Regards
Brian

 
Magento Community Magento Community
Magento Community
Magento Community
 
brianpat
Member
 
Total Posts:  44
Joined:  2008-04-05
 

Did anybody try on it?

 
Magento Community Magento Community
Magento Community
Magento Community
 
brendanb
Mentor
 
Total Posts:  1093
Joined:  2008-07-16
London, United Kingdom
 

hey brian,

thanks for getting back this post. Ill be giving this a try later today. ill let you know how it goes

thanks for your help on this one

brendan

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 4