ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Captcha parse order

March 07, 2011 7:06am

Subscribe [2]
  • #1 / Mar 07, 2011 7:06am

    mojacreative

    50 posts

    I’m doing a bit of a non standard implementation of Captch with a contact form (using Freeform).

    We get quite a bit of spam via our form, but the nature of it’s implementation makes it very undesirable to use a Captcha. As nearly all our genuine form submissions come from the same country, but all the spam comes from from elsewhere. So I figured a simple fix would be to only display the captcha to visitors from outside the target country.

    So in have tried using the Countries plugin to test where the visitor is from then conditionally displaying the captcha as a result:

    {if "{exp:country}" == "au"}
        {if captcha}
            <div class="captcha">
                <label>Please enter the word below</label>
    
                <input type="text" name="captcha" value="{captcha_word}" size="20" maxlength="10" class="field" /><br >{captcha}
                <input type="hidden" name="captcha_fired" value="yes" />
            </div>
        {/if}
    {/if}

    Pretty straight forward stuff, with the addition of a hidden field to let me know if captcha was shown for a form submission, so I know if they are defeating the captcha via some means.

    But I’m running into issues with the parse order by the looks of it. Using this method, the captcha won’t display if for my target country, but I still get the error saying I needed to complete the captcha on submission.

    So my guess is the captcha is parsed prior to the conditional, then when the conditional is parsed, the captcha ui isn’t displayed but EE still expects the input?

    Assuming this is the case, anyone got any ideas how I can work around it? Took a bit of a wild stab at putting the captcha in an inbedded template, but as I thought, no joy.

    Any help would be appreciated.

    Thanks

  • #2 / Mar 07, 2011 10:09am

    Sue Crocker

    26054 posts

    Hi, neekster..

    Keeping to the concept of using a captcha - have you looked around on Devot-ee.com to see if there are any captcha alternatives that might be easier to implement for you?

  • #3 / Mar 07, 2011 10:24am

    mojacreative

    50 posts

    Not really, as I’m perfectly happy with the default captcha in this case as it performs fine in our case. As I’m introducing usability by not displaying it to pretty much all the target market, the the core issue is if there is any way I haven’t thought of to get round the parse order?

    It did occur to me that maybe there are some extensions out there that don’t get parsed later, but looking at the hooks used by them as I understand is just replacing the default EE captcha.

    Thanks

  • #4 / Mar 07, 2011 3:56pm

    Ingmar

    29245 posts

    But I’m running into issues with the parse order by the looks of it. Using this method, the captcha won’t display if for my target country, but I still get the error saying I needed to complete the captcha on submission.

    Well, yes: if you are using captchas then EE expects them and simply hiding the image won’t change that fact. I am not aware of a built-in method to show captchas only to visitors from certain countries; there might well be third-party add-ons to allow for something like that, though.

  • #5 / Mar 07, 2011 11:22pm

    mojacreative

    50 posts

    Well I was attempting more than just hiding the image. If you remove the Country plugin from the situation and boil it down to a simple if logged in conditional you have:

    {if logged_in}
    {if captcha}
            <div class="captcha">
                <label>Please enter the word below</label>
    
                <input type="text" name="captcha" value="{captcha_word}" size="20" maxlength="10" class="field" /><br >{captcha}
                <input type="hidden" name="captcha_fired" value="yes" />
            </div>
        {/if}
    {/if}

    This code would work perfectly if it wasn’t for the fact that EE is parsing the captcha code before the conditional. Which brings me back to my question, is there any way I haven’t thought of tow work around this?

    After doing some more testing it seems to indicate that Captchas are parsed before simple conditionals, so I’m guessing I’m out of luck, is there anything else early in the parsing that you think I could look into? There is just not all that much detailed information on parsing order when it gets down to the finer details like this, which is why I wanted to check in with you guys in case there was something I’m missing.

    Thanks

  • #6 / Mar 07, 2011 11:32pm

    mojacreative

    50 posts

    Oh bingo, I think I just worked out in my head at least a way of doing it… this is the kind of workaround I was talking about 😊

    Can’t test it out yet, but I think if I put the whole form into an embed, but have two separate embeds, one with my captcha and one without, then pop the conditional in the template to load one of my two templates, that should work around the parse order in my mind. I’ll test it out later but pretty sure that will work so don’t waste anymore time on this one, thanks 😊

  • #7 / Mar 08, 2011 8:56am

    Sue Crocker

    26054 posts

    Sounds good to me.. if you do find a solution that might help others, feel free to add it to the EEWiki. 😊 Feel free to start a new thread if you have any more questions.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases