summaryrefslogtreecommitdiffstats
path: root/system/helpers/captcha_helper.php
diff options
context:
space:
mode:
authorash <ashleybaker@gmail.com>2013-04-10 13:30:12 +0200
committerash <ashleybaker@gmail.com>2013-04-10 13:30:12 +0200
commit1aa336d53b686c07291bda4f8e9dd8ac23614fc3 (patch)
treef437ff48067359a2228e09748a6fd12861616a69 /system/helpers/captcha_helper.php
parent5eb1cbfa673bfa4b8a66ab8a56389a279c1f975b (diff)
Add options in create_captcha() to specify the randomly generated captcha word length and character pool
Uses the same defaults as were hard coded in (8 chars in length, 0-9a-aA-Z). Small change in this file means less code elsewhere when generating random character strings for the captcha word.
Diffstat (limited to 'system/helpers/captcha_helper.php')
-rw-r--r--system/helpers/captcha_helper.php21
1 files changed, 16 insertions, 5 deletions
diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php
index 78e255a15..fe6c340be 100644
--- a/system/helpers/captcha_helper.php
+++ b/system/helpers/captcha_helper.php
@@ -51,7 +51,7 @@ if ( ! function_exists('create_captcha'))
*/
function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
{
- $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
+ $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'captcha_word_length' => 8, 'character_pool_for_generted_word' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
foreach ($defaults as $key => $val)
{
@@ -72,6 +72,17 @@ if ( ! function_exists('create_captcha'))
return FALSE;
}
+
+
+ // -----------------------------------
+ // Make sure captcha max length is a valid/realistic value.
+ // -----------------------------------
+
+ $captcha_word_length = (int) $captcha_word_length;
+ if ($captcha_word_length < 4) { $captcha_word_length = 4;}
+ if ($captcha_word_length > 15) { $captcha_word_length = 15; }
+
+
// -----------------------------------
// Remove old images
// -----------------------------------
@@ -95,11 +106,10 @@ if ( ! function_exists('create_captcha'))
if (empty($word))
{
- $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$word = '';
- for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++)
+ for ($i = 0, $mt_rand_max = strlen($character_pool_for_generted_word) - 1; $i < $captcha_word_length; $i++)
{
- $word .= $pool[mt_rand(0, $mt_rand_max)];
+ $word .= $character_pool_for_generted_word[mt_rand(0, $mt_rand_max)];
}
}
elseif ( ! is_string($word))
@@ -206,4 +216,5 @@ if ( ! function_exists('create_captcha'))
}
/* End of file captcha_helper.php */
-/* Location: ./system/helpers/captcha_helper.php */ \ No newline at end of file
+/* Location: ./system/helpers/captcha_helper.php */
+