非常教程

Codeigniter 3参考手册

CAPTCHA

CAPTCHA Helper

CAPTCHA帮助程序文件包含帮助创建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图像库。
  • 只有img_pathimg_url是必需的。
  • 如果没有提供一个单词,该函数将生成一个随机的ASCII字符串。你可以把你自己的词库放在一起,你可以从中随机抽取。
  • 如果不指定TRUE TYPE字体的路径,则将使用本机丑陋的GD字体。
  • “captcha”目录必须是可写的
  • 到期(秒)表示的图像将多久保留在验证码文件夹之前将被删除。默认值是两个小时。
  • word_length默认为8,默认为'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  • font_size默认为16,原生GD字体的大小限制。指定更大尺寸的“真实类型”字体。
  • img_id将被设置为验证码图像的“身份证”。
  • 如果任何颜色值丢失,它将被替换为默认值。

添加数据库

为了使验证码功能防止某人提交,您需要添加从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的数据数组
  • $ img_path字符串) - 在中创建图像的路径
  • $ img_url字符串) - CAPTCHA图像文件夹的URL
  • $ font_path字符串) - 字体的服务器路径
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

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

CAPTCHA相关

Codeigniter 3

CodeIgniter 是一个PHP MVC框架,特点是超轻量级、有数据加密、有灵活URI路由等。对于 PHP 程序员来说,它小巧但功能强大。

主页 https://codeigniter.com/
源码 https://github.com/bcit-ci/CodeIgniter
版本 3
发布版本 3.1.5