yygcom 2008-6-23 14:22
[getcode_helper.php]验证码函数
说明:
1,将[color=DarkSlateBlue]getcode_helper.php[/color] 解压到 [color=DarkSlateBlue]system\application\helpers[/color] 。
2,调用 [color=DarkSlateBlue]$this->load->helper('getcode');[/color]
3,示例
#Controller
#show.php
[code=PHP]
<?
class show extends Controller{
function __construct()
{
parent::Controller();
$this->load->helper('url');
$this->load->helper('getcode');
}
function d(){
echo "<img src=/scode.gif><form method='post' action='ck'><input name='ccd'><input type='submit'></form>";
}
function scode(){
code();
}
function ck(){
$str=$_POST['ccd'];
if(chk_code($str)){echo "ok";}
else {show_error('验证码错误');}
}
}
?>
[/code]
#routers.php
[code=PHP]
$route['([a-z0-9]*).gif'] = "show/$1"; //在url路由中添加这条规则
[/code]
效果图:
[attach]198[/attach]图一
[[i] 本帖最后由 yygcom 于 2008-6-23 14:31 编辑 [/i]]
cocely 2008-7-11 13:42
[code=PHP]<?php
//生成验证码图片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用
session_start();//将随机数存入session中
$_SESSION['authcode']="";
$im = imagecreate(62,20); //制定图片背景大小
$black = ImageColorAllocate($im, 0,0,0); //设定三种颜色
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)
while(($authcode=rand()%100000)<10000);
//将四位整数验证码绘入图片
$_SESSION['authcode']=$authcode;
imagestring($im, 5, 10, 3, $authcode, $black);
// 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。
//如果 font 是 1,2,3,4 或 5,则使用内置字体
for($i=0;$i<200;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
?>
//这个验证码也不错,没有写成helper,大家需要的自己改吧,呵呵[/code]
lxylxy888666 2008-9-10 18:39
我怎么弄出来的图片不显示哈,
有没有调试过的朋友,发下代码:handshake
yygcom 2008-9-24 16:11
特别说明
如果遇到出错,一般都是由于BOM的问题引发的,至于BOM的问题请自行GOOLE一下
heroicq 2008-9-26 09:53
楼主那个getcode_helper下载下来是CI首页的,不是验证码的助手函数??
cocely的那个代码我改成helper,可以放在helper下:
[code=PHP]<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//-------------------------------------
// 文件说明:生成验证码
//-------------------------------------
if ( ! function_exists('getcode')){
function getcode() {
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用
session_start();//将随机数存入session中
$_SESSION['authcode']="";
$im = imagecreate(62,20); //制定图片背景大小
$black = ImageColorAllocate($im, 0,0,0); //设定三种颜色
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)
while(($authcode=rand()%100000)<10000);
//将四位整数验证码绘入图片
$_SESSION['authcode']=$authcode;
imagestring($im, 5, 10, 3, $authcode, $black);
// 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。
//如果 font 是 1,2,3,4 或 5,则使用内置字体
for($i=0;$i<200;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
}
}
?>[/code]