65.9K
CodeProject 正在变化。 阅读更多。
Home

如何:使用 PHP Fusion 7 CAPTCHA。

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.85/5 (3投票s)

2008年12月26日

GPL3

1分钟阅读

viewsIcon

63842

downloadIcon

789

展示如何使用 PHP Fusion 7 CAPTCHA 类。

引言

PHP Fusion 7 拥有一个基于 GPL 许可的优秀的 CAPTCHA 类,因此我们可以在我们的 GPL 项目中使用它。
本文将展示如何使用它。

使用代码

首先,要使用 PHP Fusion 7 CAPTCHA 类,我们需要创建一个数据库和一个用于存储 CAPTCHA 代码的表。
我使用如下 SQL 创建了它们:
/* Creating the DataBase */
CREATE DATABASE `phpfusion7`;

/* Creating the CAPTCHA table */
DROP TABLE IF EXISTS `phpfusion7`.`fusion_captcha`;
CREATE TABLE  `phpfusion7`.`fusion_captcha` (
  `captcha_datestamp` int(10) unsigned NOT NULL default '0',
  `captcha_ip` varchar(20) NOT NULL,
  `captcha_encode` varchar(32) NOT NULL default '',
  `captcha_string` varchar(15) NOT NULL default '',
  KEY `captcha_datestamp` (`captcha_datestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
如果您要更改数据库或表的名称,您还应该更改类文件中的一些信息。(例如 config.phpmultisite_include.php
然后,我们必须创建一个包含数据库设置的 PHP 文件。我使用了 PHP Fusion 的默认文件。
在 CAPTCHA 类中,我们将包含它。
config.php
<?php
    // database settings
    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "phpFusion7";
    $db_prefix = "fusion_";
    define("DB_PREFIX", "fusion_");
?>
我也已经将此文件复制到我的项目中(multisite_include.php)。
此文件存在于 PHP Fusion 项目中,包含数据库表的名称。

PHP Fusion 7 中的 securimage.php 包含 CAPTCHA 类。
现在让我们看看这个 CAPTCHA 类的调整变量。
 var $image_width = 140; //Width of CAPTCHA image
var $image_height = 45;//Height of CAPTCHA image
//Type of CAPTCHA image
//SI_IMAGE_PNG for PNG image
//SI_IMAGE_GIF for GIF image
//SI_IMAGE_JPEG for JPG image
var $image_type = SI_IMAGE_PNG; 
var $code_length = 4; // length of CAPTCHA code that'll show in image
//All characters that'll use for creating CAPTCHA code
var $charset = 'ABCDEFGHKLMNPRSTUVWYZ23456789'; 
//If you want, this class uses the characters that store in a file, set this variable
//and set $use_wordlist to true
var $wordlist_file = '';
var $use_wordlist  = true;
//For using GD fonts that store in gdfonts folder
var $use_gd_font = false;
//Name of destination GD font 
var $gd_font_file = 'gdfonts/bubblebath.gdf';
var $gd_font_size = 20;//Size of GD font
var $ttf_file = "./elephant.ttf";//Path of TTF Font file
var $font_size = 24;//Size of TTF font
//Maximum and minimum angle of CAPTCHA code
//The CAPTCHA code will rotate with these angles 
var $text_angle_minimum = -20;
var $text_angle_maximum = 20;
//Start of X posiotion for writing CAPTCHA code
var $text_x_start = 8;
//Maximum and minimum of distance between CAPTCHA words 
var $text_minimum_distance = 30;
var $text_maximum_distance = 33; 
var $image_bg_color = "#e3daed";//Background of CAPTCHA image 
var $text_color = "#ff0000";//Text color of CAPTCHA image
//If you set this one to true the CAPTCHA code will show colorful else will show in red
var $use_multi_text = true;
//Colors for coloring the words in CAPTCHA image
var $multi_text_color = "#0a68dd,#f65c47,#8d32fd";
//For showing the CAPTCHA code with transparency
var $use_transparent_text = true; 
var $text_transparency_percentage = 50;//Percent of CAPTCHA code transparency
var $draw_lines = true;//For drawing lines in CAPTCHA image
var $line_color = "#80BFFF"; //Color of lines
var $line_distance = 5; //The distance between lines
var $line_thickness = 1; //Thickness of lines
var $draw_angled_lines = false;//Draw lines with a angle
var $draw_lines_over_text = false;
var $arc_linethrough = true; //Draw two arcs over the words in the CAPTCHA image
var $arc_line_colors = "#8080ff";//Color of arcs 
var $audio_path = './audio/';//Path of audio files for
一些调整的结果

    var $text_angle_minimum = -50;
    var $text_angle_maximum = 50;
php Fusion Captcha
    var $text_minimum_distance = 10;
    var $text_maximum_distance = 15;
php Fusion Captcha
    var $use_multi_text = false;
php Fusion Captcha
    var $use_transparent_text = true;
    var $text_transparency_percentage = 80;
php Fusion Captcha
    var $draw_lines = false;
php Fusion Captcha
    var $draw_lines = true;
    var $line_color = "#80BFFF";
    var $line_distance = 5;
    var $line_thickness = 1;
    var $draw_angled_lines = true;
    var $draw_lines_over_text = false;
php Fusion Captcha
    var $draw_lines = true;
    var $line_color = "#80BFFF";
    var $line_distance = 5;
    var $line_thickness = 1;
    var $draw_angled_lines = false;
    var $draw_lines_over_text = true;
php Fusion Captcha
var $arc_linethrough = false;
php Fusion Captcha

好的,现在我们可以使用这个类了。
securimage_show.php 生成 CAPTCHA 图像。我们只需要正确使用它。
像这样:
<script language="javascript" type="text/javascript">
  function RefreshCaptcha()
  {
  var captchaImage = document.getElementById('captcha');
  captchaImage.src = 'securimage/securimage_show.php?sid=' + Math.random(); 
  return false
  }
  </script>
<img src="securimage/securimage_show.php?sid=' + 
		Math.random();" alt="Validation Code:" 
  name="captcha" width="145" height="45" align="left" id="captcha"/> 
  <a onclick="RefreshCaptcha();" href="#"> 
  <img align="top" alt="" src="securimage/images/refresh.gif"/> </a> 
  <a href="securimage/securimage_play.php"> 
  <img align="top" style="margin-bottom: 1px;" alt="" 
		src="securimage/images/audio_icon.gif"/> 
  </a>
之后,我们可以使用如下 PHP 代码通过 mysql 查询访问 CAPTCHA 代码:
getCode.php
<?php
function getCode()
{ 
    require_once("config.php");
    require_once("securimage/multisite_include.php"); 
    
    mysql_connect($db_host, $db_user, $db_pass);
    mysql_select_db($db_name);
    
    $result = mysql_query("SELECT MAX(captcha_datestamp),
    		captcha_string FROM ".DB_CAPTCHA." WHERE captcha_ip='" . 
            	$_SERVER['REMOTE_ADDR'] . "'") 
    	or die(mysql_error());
    if (mysql_num_rows($result)) 
    {
        $data = mysql_fetch_assoc($result);
        return strtoupper($data['captcha_string']);
    } 
    else 
    {
    	return "";
    }
}
$captchaCode = getCode();
echo $captchaCode;
?>

我的其他 CAPTCHA 文章:
PHP 中的 Captcha 图像
如何使用:Phpbb3 captcha

历史

首次发布:2008 年 12 月 26 日

© . All rights reserved.