Changeset 3043 for trunk/application

Show
Ignore:
Timestamp:
07/10/2008 04:35:52 PM (5 months ago)
Author:
Geert
Message:

Updated the Captcha example to show how you could use the response counters

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/application/controllers/examples.php

    r3031 r3043  
    175175        public function captcha() 
    176176        { 
     177                // Look at the counters in the Session Profiler 
     178                new Profiler; 
     179 
     180                // Ban bots after 10 invalid responses 
     181                // Be careful not to ban real people though! Set the threshold high enough. 
     182                if (Captcha::invalid_response_count() > 9) 
     183                        exit('Bye! Stupid bot.'); 
     184 
    177185                // Form submitted 
    178                 if (isset($_POST['captcha_response'])) 
    179                 { 
    180                         // Note: Captcha::valid() can be used in conjunction with the Validation library too 
    181                         echo (Captcha::valid($_POST['captcha_response'])) ? 'Good answer!' : 'Wrong answer!'; 
    182                 } 
    183  
    184                 // Show form 
     186                if ($_POST) 
     187                { 
     188                        // User has not given three valid responses yet 
     189                        if (Captcha::valid_response_count() < 3) 
     190                        { 
     191                                // Valid response has been submitted 
     192                                if (Captcha::valid_response($this->input->post('captcha_response'))) 
     193                                { 
     194                                        echo '<p style="color:green">Good answer!</p>'; 
     195                                } 
     196                                else 
     197                                { 
     198                                        echo '<p style="color:red">Wrong answer!</p>'; 
     199                                } 
     200                        } 
     201 
     202                        // Validate other fields here 
     203                } 
     204 
     205                // Open form 
    185206                echo form::open(); 
    186                 echo '<p>'; 
    187                 echo Captcha::factory()->render(); // <-- shows the Captcha challenge (image/riddle/etc) 
    188                 echo '</p>'; 
    189                 echo form::input('captcha_response'); 
     207                echo '<p>Other form fields here...</p>'; 
     208 
     209                // Don't show Captcha anymore after three or more valid responses 
     210                if (Captcha::valid_response_count() < 3) 
     211                { 
     212                        echo '<p>'; 
     213                        echo Captcha::factory()->render(); // Shows the Captcha challenge (image/riddle/etc) 
     214                        echo '</p>'; 
     215                        echo form::input('captcha_response'); 
     216                } 
     217 
     218                // Close form 
    190219                echo form::submit(array('value' => 'Check')); 
    191220                echo form::close();