CodeIgniter

captchaとは?

CAPTCHA - Wikipedia

CAPTCHA(キャプチャ)は チャレンジ/レスポンス型テストの一種で、応答者がコンピュータでないことを確認するために使われる。
この用語はカーネギーメロン大学のLuis von Ahn、マヌエル・ブラム、Nicholas J. Hopper、IBMのJohn Langfordによって2000年に造られた。
CAPTCHAという語は「Completely Automated Public Turing test to tell Computers and Humans Apart」(コンピュータと人間を区別する完全に自動化された公開チューリングテスト)の人為的頭文字である。

captcha-antirobot.jpg

リンク

CAPTCHA ヘルパ

CodeIgniterのcaptchaヘルパーを使って、captcha画像を生成します。

ヘルパのロード

captchaヘルパー関数を使う前に、あらかじめcaptchaヘルパーを呼び出しておきます。

<?php $this->load->helper('captcha'); ?>

capthca用のデータベーステーブル

captcha情報を一時的にデータベースに保存するため、captcha用のテーブルを用意します。

  • MySQLの場合
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
    
    -
    |
    |
    |
    |
    |
    |
    !
    
    CREATE TABLE captcha (
     captcha_id bigint(13) unsigned NOT NULL auto_increment,
     captcha_time int(10) unsigned NOT NULL,
     ip_address varchar(16) default '0' NOT NULL,
     word varchar(20) NOT NULL,
     PRIMARY KEY `captcha_id` (`captcha_id`),
     KEY `word` (`word`)
    );

設定

captcha画像を生成する元データを設定します。

  1
  2
  3
  4
  5
  6
  7
  8
<?php $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任意captcha画像に表示される文字列。指定されない場合はランダムなASCII文字列が生成される。
img_path必須captcha画像が保存されるディレクトリー。書き込み可能でなければいけない。
img_url必須captcha画像のURL
font_path任意capthca画像に使用するTrueTypeフォントのパス。デフォルトではGDフォントが使用される。
img_width任意captcha画像の横幅。単位:ピクセル
img_height任意captcha画像の高さ。単位:ピクセル
expiration任意有効期限。captcha画像が削除されるまでの時間。単位:秒。デフォルトでは7200秒。

captcha画像生成

captchaヘルパーのcreate_captcha()関数で、captcha画像を生成して、利用します。

  • CodeIgniterのコントローラーにおける記述例
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
    
    <?php // load
    $this->load->helper('captcha');
     
    // config
    $vals = array(
        'img_path' => './captcha/',
        'img_url' => 'http://example.com/captcha/'
    );
     
    // create
    $cap = create_captcha($vals);
     
    // DB
    $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);
     
    // view
    echo 'Submit the word you see below:';
    echo $cap['image'];
    echo '<input type="text" name="captcha" value="" />'; ?>
  • 送信を受け付ける側のページの記述例
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
    
    <?php // delete old captcha 期限切れのキャプチャを削除
    $expiration = time()-7200; // 有効期限: 2時間
    $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
     
    // check キャプチャが存在するか確認
    $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND date > ?";
    $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";
    } ?>

添付ファイル: filecaptcha-antirobot.jpg 1092件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2011-07-31 (日) 15:37:07 (4645d)