Changeset 3080
- Timestamp:
- 07/11/2008 01:30:46 PM (5 months ago)
- Location:
- trunk
- Files:
-
- 3 modified
-
application/controllers/examples.php (modified) (3 diffs)
-
system/config/captcha.php (modified) (2 diffs)
-
system/libraries/Captcha.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/application/controllers/examples.php
r3047 r3080 175 175 public function captcha() 176 176 { 177 // Look at the counters in the Session Profiler 177 // Look at the counters for valid and invalid 178 // responses in the Session Profiler. 178 179 new Profiler; 179 180 180 // Load Captcha library 181 // Load Captcha library, you can supply the name 182 // of the config group you would like to use. 181 183 $captcha = new Captcha; 182 184 183 // Ban bots (that accept session cookies) after 50 invalid responses 185 // Ban bots (that accept session cookies) after 50 invalid responses. 184 186 // Be careful not to ban real people though! Set the threshold high enough. 185 187 if ($captcha->invalid_count() > 49) … … 189 191 if ($_POST) 190 192 { 191 // User has not given three valid responses yet192 if ( $captcha->valid_count() < 3)193 // Captcha::valid() is a static method that can be used as a Validation rule also. 194 if (Captcha::valid($this->input->post('captcha_response'))) 193 195 { 194 // Captcha::valid() is a static method that can be used as a Validation rule also 195 if (Captcha::valid($this->input->post('captcha_response'))) 196 { 197 echo '<p style="color:green">Good answer!</p>'; 198 } 199 else 200 { 201 echo '<p style="color:red">Wrong answer!</p>'; 202 } 196 echo '<p style="color:green">Good answer!</p>'; 203 197 } 198 else 199 { 200 echo '<p style="color:red">Wrong answer!</p>'; 201 } 204 202 205 203 // Validate other fields here 206 204 } 207 205 208 // Openform206 // Show form 209 207 echo form::open(); 210 208 echo '<p>Other form fields here...</p>'; 211 209 212 // Don't show Captcha anymore after three or more valid responses 213 if ($captcha->valid_count() < 3) 210 // Don't show Captcha anymore after the user has given enough valid 211 // responses. The "enough" count is set in the captcha config. 212 if ( ! $captcha->promoted()) 214 213 { 215 214 echo '<p>'; … … 217 216 echo '</p>'; 218 217 echo form::input('captcha_response'); 218 } 219 else 220 { 221 echo '<p>You have been promoted to human.</p>'; 219 222 } 220 223 -
trunk/system/config/captcha.php
r3046 r3080 14 14 * background - Path to background image file 15 15 * font - Path to font file 16 * promote - Valid response count threshold to promote user (FALSE to disable) 16 17 */ 17 18 $config['default'] = array … … 23 24 'background' => '', 24 25 'font' => SYSPATH.'fonts/DejaVuSerif.ttf', 26 'promote' => FALSE, 25 27 ); -
trunk/system/libraries/Captcha.php
r3065 r3080 27 27 'background' => '', 28 28 'font' => '', 29 'promote' => FALSE, 29 30 ); 30 31 … … 171 172 static $counted; 172 173 174 // User has been promoted, always TRUE and don't count anymore 175 if (self::instance()->promoted()) 176 return TRUE; 177 173 178 // Challenge result 174 179 $result = (sha1(strtoupper($response)) === Session::instance()->get('captcha_response')); … … 253 258 254 259 /** 260 * Checks whether user has been promoted after having given enough valid responses. 261 * 262 * @return boolean 263 */ 264 public function promoted() 265 { 266 // Promotion has been disabled 267 if (self::$config['promote'] === FALSE) 268 return FALSE; 269 270 // Compare the valid response count to the threshold 271 return ($this->valid_count() >= self::$config['promote']); 272 } 273 274 /** 255 275 * Output the Captcha challenge. 256 276 *
