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

Implement reCaptcha
 
rareus
Jr. Member
 
Total Posts:  17
Joined:  2012-04-06
 

Hi guys,

I’m trying to implement reCaptcha on my contact us page. Apparently I’m able to implement the widget, but the verification/validation isn’t working. Thus users can simply submit the form without filling in the reCaptcha.
Based on reCaptcha resources, I need to create a page verify.php, however since it’s already a form, i’m not sure how I can direct the reCaptcha to verify.php without affecting the email sending feature of the page.

Here’s the code for “contact us” page:

<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
<
div class="page-title">
    <
h1><?php echo Mage::helper('contacts')->__('Contact Us'?></h1>
</
div>
<
form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">
    <
div class="fieldset">
        <
h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information'?></h2>
        <
ul class="form-list">
            <
li class="fields">
                <
div class="field">
                    <
label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name'?></label>
                    <
div class="input-box">
                        <
input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                    </
div>
                </
div>
                <
div class="field">
                    <
label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email'?></label>
                    <
div class="input-box">
                        <
input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->htmlEscape($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
                    </
div>
                </
div>
            </
li>
            <
li>
                <
label for="telephone"><?php echo Mage::helper('contacts')->__('Telephone'?></label>
                <
div class="input-box">
                    <
input name="telephone" id="telephone" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text" type="text" />
                </
div>
            </
li>
            <
li>
            <
div class="field">
                <
label for="subject" class"required"><em>*</em><?php echo Mage::helper('contacts')->__('Subject'?></label>
                <
div class="input-box">
                    <
select name="subject" id="subject" title="<?php echo Mage::helper('contacts')->__('Subject') ?>" style="width:200px" class="input-text required-entry">
                       <
option value="" selected></option>
                    <
option value="value1">Order Enquiry</option>
                    <
option value="value2">Warranty</option>
                    <
option value="value3">Wholesale Reseller</option>
                    <
option value="value4">General Enquiry</option>
                    <
option value="value5">Webmaster</option>
                </
select>
                </
div>
            </
div>
            </
li>
            <
li class="wide">
                <
label for="comment" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Comment'?></label>
                <
div class="input-box">
                    <
textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3"></textarea>
                </
div>
            </
li>
        </
ul>
    </
div>
    <
form method="post" action="verify.php">
    
<?php
require_once('recaptchalib.php');
  
$publickey "6LcwzdESAAAAALmo6y-gnf79KSNsD8wiur_G1Yqb"// you got this from the signup page
  
echo recaptcha_get_html($publickey);
        
?>
        
</form>
        
    <
div class="buttons-set">
        <
class="required"><?php echo Mage::helper('contacts')->__('* Required Fields'?></p>
        <
input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <
button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit'?></span></span></button>
    </
div>
</
form>
<
script type="text/javascript">
//<![CDATA[
    
var contactForm = new VarienForm('contactForm'true);
//]]>
</script>

Here’s the code for verify.php:

<?php
  
require_once('recaptchalib.php');
  
$privatekey "6LcwzdESAAAAAG1rhS3OiuUjXVsFMC6FPpo7IsPN";
  
$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
  
}
  ?>

I’ve also attached the recaptchalib.php for reference. Please assist as I’m struggling throughout the night for this! Thanks!

File Attachments
recaptchalib.php  (File Size: 10KB - Downloads: 47)
 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello.
Instead of doing it yourself, why don’t you try an extension.
I recommend this one
It worked smoothly for me.

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rareus
Jr. Member
 
Total Posts:  17
Joined:  2012-04-06
 
tzyganu - 23 May 2012 10:33 PM

Hello.
Instead of doing it yourself, why don’t you try an extension.
I recommend this one
It worked smoothly for me.

Marius.

Hey Marius,

Thanks for the reply! However I can’t seem to get it working for my site, I’m using 1.6.2.0
When I try to access the extension, all I get was a 404 page in the frame.

Edit: Ok I logged out and log in again, it’s working! Now trying out. Thanks for the help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
hiroprotagonist
Jr. Member
 
Total Posts:  23
Joined:  2012-01-22
 

Hi all,

I didn’t want to open new thread for CE 1.7, but I’m wondering if there is a way to include non-default pages in recaptcha ? I had succcessfully used the Fontis extension in 1.6.2. Which files need to be adapted for more pages to be “captcha’ed” in 1.7 ? I couldn’t find any info on this .

Thanks in advance,
hiro

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

In theory you can make any page to support captcha. (At least the ones that have forms, because for the others there is no point).
You just need some programming skills.
You need to check how captcha works for the other pages and do the same for your custom page.

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
hiroprotagonist
Jr. Member
 
Total Posts:  23
Joined:  2012-01-22
 

Marius,

Any quick \"shortcut hints\” on which files need looking at ? Just getting into Magento (programming skills no problem, but haven\’t done php before) so if you could point me in the right direction as far as the files are concerned, I\’m sure I could handle the rest smile

Cheers,
hiro

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Look in the etc/config.xml file of the extension in the ‘<rewrite>’ section to see how you can rewrite an action, then in the controllers folder to see what you need to do to for adding captcha to those actions.
And in app/design/frontend/{interface}/{theme}/layout/fontri_recaptcha.xml you can see how you can add a recaptcha block to your layout.

Good luck.
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top