We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11149
    • 17 Posts
    Hello,

    at first let me say I’m quite new to modx. I wrote an easy contact form using FormIt. Now I’d like to introduce additional spam protection.

    I want to delay the submission of a form by a certain amount of time. Meaning: When somebody opens the contact form until he sends the form, there has to be a certain amount of delay. Otherwise the form is not to be send. I’d like this check to be a custom hook.
    As I don’t have much experience in coding snippets, I’d like to ask if anybody has a solution or a hint for me.
    If we find a solution, it could be included in FormIt on the long run. Just an Idea.


    I hope I could convey my request. Any help would be much appreciated. smiley



    PS: sorry for my English. It’s not my first language.
      • 3749
      • 24,544 Posts
      If I’m understanding you, you want to reject posts that are submitted too soon after the form is shown. You’d have to store the start time (either in a hidden form field or a $_SESSION variable., then add a hook (ahead of the email hook) that checks that time against the current time.

      If it’s a simple contact form, you might look at SPForm, which has that check built in (and some other spam checks as well).
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 11149
        • 17 Posts
        Thanks for your advise. You understood me right I’d like to stay with FormIt.
        I did a little digging in your source code (SPForm). So far I came up with this hook:
        <?php
        /*'timer' is a hidden input element with the value of the first site view (startTime) */
        $startTime = $hook->getValue('timer'); 
        $currentTime = time();
        /*at least 10 Sekunden*/
        if ($startTime < ($currentTime-10)) {
          $value = true;
        } else {
          $errorMsg = 'Please wait at least 10 seconds till the form is submitted.'; 
          $hook->addError('timer',$errorMsg); 
          $value false; 
        }
        return $value;
        

        It is not working. Is there any error in the code? And is the corresponding placeholder for the error message: [[!+fi.error_timer]]?


        Does anybody know, if a special placeholder for the time of the site view exists? At the moment I use this Snippet to insert the time value in the hidden input element:
        <?php
        return time();
        



        I hope I’m not totally wrong. Any help would be appreciated, again. smiley
          • 3749
          • 24,544 Posts
          I don’t see anything wrong.

          I would try

          die($startTime . '---' . time() );


          To see what’s happening.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 11149
            • 17 Posts
            I got it working now. there was a typo in the code. grin


            For everybody interested in this hook for FormIt here is the working code:
            <?php
            /* 'timer' is a hidden input element with the value of the first site view (startTime) */
            $startTime = $hook->getValue('timer'); 
            $currentTime = time();
            /* at least 10 Sekunden */
            if ($startTime < ($currentTime-10)) {
              $value = true;
            } else {
              $errorMsg = 'Please wait at least 10 seconds till the form is submitted.';
              $hook->addError('timer',$errorMsg);
              $value = false;
            }
            return $value;
            

            To insert the start time value in the hidden input field "timer", you need this Snippet:
            <?php
            return time();
            




            Thanks Bob!
              • 3749
              • 24,544 Posts
              I’m glad you got it working. Thanks for reporting back. smiley
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting