【求助】CI开发验证码程序
初次接触CI,目前在用CI做验证码程序,大体思路还是和以前一样,通过gd生成图片,改成MVC模式不是很明白如何使用,能否有人指导一下。或者给个例程,不甚感激。 我好久没用CI, Captcha库都不记得是否使用过, 所以下面的代码调试不出来,是正常的;-) 但修正应该不是什麽难事.我不喜欢生成图片然后再删除这样的东西,自己参照别人写了这个,保存到app目录的libraries目录里,这里假设文件名是CaptchaImage.php
class CaptchaImage{
var $length;
var $string;
var $ttfFont;
var $charWidth;
//var $background_colour;
//var $text_colour;
//var $colours = array();
//var $shadow_height;
//var $shadow_dark;
/**
* PHP4 constructor.
*
* @access public
* @param string $length captcha string length
* @param string $string captcha string
* @return void
*/
function __construct($length = 7, $string = null){
$this->length = $length;
$this->ttfFont = './App/fonts/captcha.ttf';
$this->charWidth = 12;
$this->font_size = 9;
if (!isset($string)){
$this->mystring = $this->randomString($length);
} else {
$this->mystring = $string;
}
//$this->background_colour = 'FFFFFF';
//$this->text_colour = '000000';
//$this->colours = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
//$this->shadow_height = 16;
//$this->shadow_dark = true;
}
function setFont($ttfpath, $ttfsize = 8){
if(file_exists($ttfpath)){
$this->ttfFont = $ttfpath;
$this->font_size = $ttfsize;
} else {
$this->ttfFont = 2;
//echo 'font can\'t be found';
}
}
function randomString($length){
//alphanumeric array widthout ambiguous chars
$alphanumericArr= array ('A','B','C','D','E','F','G','H','L','M','P','R','2','3','8','9');
shuffle($alphanumericArr);
$alphanumericArr = array_slice($alphanumericArr, 0, $length-1);
$string = join("",$alphanumericArr);
return $string;
}
function draw(){
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: image/png");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: ".gmdate("D, d M Y H:i:s", time())." GMT");
$length = $this->length * $this->charWidth + 20;
$height = 25;
$image = imagecreate($length, $height);
$background = imagecolorallocate($image, 00, 0, 0);
$textcolor= imagecolorallocate($image, 255,255,255);
$linecolor= imagecolorallocate($image, 0, 0, 255);
$border_color = ImageColorAllocate($image, 153, 102, 102);
$this->DrawLines($image, $length, $height);
imagettftext($image, $this->font_size, 0, 4, 18,
$textcolor,
$this->ttfFont,
$this->mystring);
// -----------------------------------
//Create the border
// -----------------------------------
imagerectangle($image, 0, 0, $length-1, $height-1, $border_color);
// -----------------------------------
//Generate the image
// -----------------------------------
imagepng($image);
//echo $this->ttfFont;
exit();
}
function getString(){
return $this->mystring;
}
function _colourHex($img, $HexColorString){
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function _colourHexshadow($img, $HexColorString, $mork){
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if($mork){
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
} else {
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function DrawLines($oImage, $length, $height) {
for ($i = 0; $i < 48; $i++) {
// allocate colour
if (true) {
$iLineColour = imagecolorallocate($oImage, rand(90, 100), rand(100, 140), rand(120, 130));
} else {
$iRandColour = rand(100, 250);
$iLineColour = imagecolorallocate($oImage, $iRandColour, $iRandColour, $iRandColour);
}
//imagecolorallocatealpha($oImage, rand(90, 100), rand(100, 140), rand(120, 130), rand(100, 120));
// draw line
imageline($oImage, rand(0, $length), rand(0, 0), rand(0, $length), rand($height, $height), $iLineColour);
}
}
}
控制器
<?php if (! defined ('BASEPATH')) die('No direct script access allowed.');
class Captcha extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->view("v_captcha");
}
function draw(){
$this->load->library('captchaimage');
ob_start();
//session_start();
$this->session->set_flashdata('captcha', $this->captchaimage->getString());
$this->captchaimage->setFont('./App/fonts/captcha.ttf',25); //这个字体自己google一个,下载到你本地目录!!!
//echo file_exists($ttfpath)
$this->captchaimage->draw();
//echo time(); //needed to force image expiration
//session_write_close();
ob_flush();
// return;
}
function valid()
{
if($this->input->post("security_code")==$this->session->get("captcha")){
echo "good, u key the right words";
}else{
echo "bad man, try again.";
}
}
}
?>
视图v_captcha.php
<form method="post" action="captcha/valid">
<input type="text" name="security_code" maxlength="6"/>
<img src="./draw" title="Security Code" alt="Code" />
<button type="submit"/>
</form> 好代码啊!我给加亮一下~ 本来语法加亮这些琐事不该再劳烦老大,但每次我都不得要领,怎么加都不亮:funk: 哈哈,我的工作就是为大家服务~呵呵 $this->session->get()
Fatal error: Call to undefined method CI_Session::get() in D:\www\my\system\application\controllers\captcha.php on line 25
怎么回事 呵呵,应该是他用的不是 CI 里的 Session 类吧,你可以把这个改一下,我记得是 $this->session->userdata('xxx') 试试吧 我在Session Class 里面也找不到flashdata(),set_flashdata()這些方法 ./App/fonts/captcha.ttf
这个目录在CI里面没有啊,fonts目录是在system目录下的,不是在App目录下面吧 flash data 是 1.6.1 里面的新方法,1.6.1 的 Session 类有 flash data
页:
[1]
2