CAPTCHA Helper

CAPTCHA Helper

CAPTCHA帮助程序文件包含帮助创建CAPTCHA图像的功能。

  • 加载这个帮手

加载这个帮手

这个帮助器使用下面的代码加载:

$this->load->helper('captcha'

使用CAPTCHA助手

一旦加载,你可以生成这样的验证码:

$vals = array( 'word' => 'Random word', 'img_path' => './captcha/', 'img_url' => 'http://example.com/captcha/', 'font_path' => './path/to/fonts/texb.ttf', 'img_width' => '150', 'img_height' => 30, 'expiration' => 7200, 'word_length' => 8, 'font_size' => 16, 'img_id' => 'Imageid', 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', // White background and border, black text and red grid 'colors' => array( 'background' => array(255, 255, 255), 'border' => array(255, 255, 255), 'text' => array(0, 0, 0), 'grid' => array(255, 40, 40) ) $cap = create_captcha($vals echo $cap['image'];

  • 验证码功能需要GD图像库。

添加数据库

为了使验证码功能防止某人提交,您需要添加从create_captcha()数据库返回的信息。然后,当用户提交表单中的数据时,您需要验证数据是否存在于数据库中并且没有过期。

这是一个表格原型:

CREATE TABLE captcha ( captcha_id bigint(13) unsigned NOT NULL auto_increment, captcha_time int(10) unsigned NOT NULL, ip_address varchar(45) NOT NULL, word varchar(20) NOT NULL, PRIMARY KEY `captcha_id` (`captcha_id`), KEY `word` (`word`)

这是一个数据库使用的例子。在显示CAPTCHA的页面上,您会看到如下所示的内容:

$this->load->helper('captcha' $vals = array( 'img_path' => './captcha/', 'img_url' => 'http://example.com/captcha/' $cap = create_captcha($vals $data = array( 'captcha_time' => $cap['time'], 'ip_address' => $this->input->ip_address(), 'word' => $cap['word'] $query = $this->db->insert_string('captcha', $data $this->db->query($query echo 'Submit the word you see below:'; echo $cap['image']; echo '<input type="text" name="captcha" value="" />';

然后,在接受提交的页面上,您将拥有如下所示的内容:

// First, delete old captchas $expiration = time() - 7200; // Two hour limit $this->db->where('captcha_time < ', $expiration) ->delete('captcha' // Then see if a captcha exists: $sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?'; $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration $query = $this->db->query($sql, $binds $row = $query->row( if ($row->count == 0) { echo 'You must submit the word that appears in the image.'; }

可用功能

以下功能可用:

create_captcha([$data = ''[, $img_path = ''[, $img_url = ''[, $font_path = '']]]])

参数:$ data(array) - 用于CAPTCHA的数据数组$ img_path(字符串) - 在$ img_url中创建图像的路径(字符串) - CAPTCHA图像文件夹的URL $ font_path(字符串) - 字体的服务器路径
返回:数组('word'=> $ word,'time'=> $ now,'image'=> $ img)
返回类型:排列

  • $ dataarray) - CAPTCHA的数据数组

Returns: array(‘word’ => $word, ‘time’ => $now, ‘image’ => $img)

返回类型:数组

获取一系列信息以生成CAPTCHA作为输入,并根据您的规范创建图像,并返回关于图像的一组关联数据。

array( 'image' => IMAGE TAG 'time' => TIMESTAMP (in microtime) 'word' => CAPTCHA WORD )

图像是实际的图像标签:

<img src =“http://example.com/captcha/12345.jpg”width =“140”height =“50”/>

时间是用作不文件扩展名的图像名称微时间戳。这将是一个这样的数字:1139612155.3422

是在出现的验证码图像,其中,如果未提供的功能,可以是随机符串中的