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

    I have a captcha problem that gradually drives me crazy. It's a simple script that only works on not-MODX-php-files. As soon as I implement it in MODX I get error messages in different browsers whereas in some browsers it works - but only temporarly. After a few reloads of the page it doesn't work anymore an vice versa.

    This is the captcha call within the contact resource:

    <section>
    	<label class="label">Enter characters below:</label>
    	<label class="input input-captcha">
    	     <img src="assets/plugins/forms/captcha/image.php?[[!generateCaptcha]]" width="100" height="35" alt="Captcha image" />
    	     <input type="text" maxlength="6" name="captcha" id="captcha">
    	</label>
    </section>



    A pretty simple snippet ("generateCaptcha"):

    echo time();


    All script files are loaded correctly.

    The php call on the top of the page:

    <?php
    
    // Make the page validate
    ini_set('session.use_trans_sid', '0');
    
    // Create a random string, leaving out 'o' to avoid confusion with '0'
    $char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 4));
    
    // Concatenate the random string onto the random numbers
    // The font 'Anorexia' doesn't have a character for '8', so the numbers will only go up to 7
    // '0' is left out to avoid confusion with 'O'
    $str = rand(1, 7) . rand(1, 7) . $char;
    
    // Begin the session
    session_start();
    
    // Set the session contents
    $_SESSION['captcha_id'] = $str;
    
    ?>


    I put the code in a snippet and the snippet call in the contact page template: [[!ContactForm]]


    The validation script:

    <script type="text/javascript">
    			$(function()
    			{
    				// Validation
    				$("#form").validate(
    				{					
    					// Rules for form validation
    					rules:
    					{	
    						[...]
    						captcha:
    						{
    							required: true,
    							remote: 'assets/plugins/forms/captcha/process.php'
    						}
    					},
    										
    					// Messages for form validation
    					messages:
    					{
    						[...]
    
    						captcha:
    						{
    							required: 'Please enter characters',
    							remote: 'Correct captcha is required'
    						}
    					},
    										
    					// Ajax form submition					
    					submitHandler: function(form)
    					{
    						$(form).ajaxSubmit(
    						{
    							beforeSend: function()
    							{
    								$('#form button[type="submit"]').attr('disabled', true);
    							},
    							success: function()
    							{
    								$("#form").addClass('submited');
    							}
    						});
    					},
    					
    					// Do not change code below
    					errorPlacement: function(error, element)
    					{
    						error.insertAfter(element.parent());
    					}
    				});
    			});			
    		</script>				
    




    If I use all the codes above in an ordinary standalone php file the captcha works correctly. So it's not a mistake in the code but in the code within MODX. Is something wrong with the snippet or the snippet call? I tried it cached and uncached. The cache of the contact resource is active. I emptied core/cache and the browser caches.

    Does anybody have an idea?

    Thanks!!!
    [ed. note: carlina last edited this post 10 years, 9 months ago.]
      • 3749
      • 24,544 Posts


      Why not use the MODX Captcha extra, or at least copy its code?
        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
        • 4628
        • 58 Posts
        Mmmh, I prefered an all-in-one solution because for me (a non-developper) it's easier to handle (honest answer).

        I installed the plugin, activated it in the system settings and inserted

        <img src="[[++assets_url]]components/captcha/captcha.php" alt="Captcha Image" />


        But I don't know what to do next. I can see the captcha image. But how do I make it validate? And what about the error message after typing a wrong code? I don't know how to "connect" the captcha plugin with my form script :-/
          • 3749
          • 24,544 Posts
          The plugin writes a $_SESSION variable ($_SESSION['veriword']) with the correct answer when it generates the image, so you need a snippet that checks that against the user's input. The user's answer will be in the $_POST array under the name of the Captcha input.

          So, if the name of the Captcha input is 'captcha_answer', you could do this:

          $captcha_ok = false;
          
          $answer = $modx->getOption('captcha_answer', $_POST, '');
          
          if ($answer == $_SESSION['veriword']) {
            $captcha_ok = true;
          }
          


          Then check the $captcha_ok variable in the rest of your code and act accordingly.
            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
            • 4628
            • 58 Posts
            Wow! Thank you very much!!!
              • 3749
              • 24,544 Posts
              Don't thank me until it works. wink
                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