code.php 这个文件<?php
session_start();
//定义个常量,用来授权调用includes里面的文件
define('IN_TG',true);
//引入公共文件
require dirname(__FILE__).'/includes/common.inc.php'; //此文件中引用了 global.func.php 文件
//调用方法生成验证码
_code();-----------------------------以下是global.func.php文件-------------------------------------------------------- /**
* _code()是验证码函数
* @access public
* @param int $width 表示验证码的长度
* @param int $height 表示验证码的高度
* @param int $_rnd_code 表示验证码的位数
* @param bool $_flag 表示验证码是否需要边框
* @return void 这个函数执行后产生一个验证码
*/
function _code($width=75,$height=25,$_rnd_code=4,$_flag=false)
{
//随机码的个数
$_rnd_code=4;
//随机产生验证码
$_nmsg="";
for($i=0;$i<$_rnd_code;$i++)
{
$_nmsg .=dechex(mt_rand(0,15));
}
//将验证码保存在session中
$_SESSION['code']=$_nmsg;
//创建一张图像
$image=imagecreatetruecolor($width,$height);//背景颜色
$_white=imagecolorallocate($image,255,255,255);
//填充颜色
imagefill($image,0,0,$_white);
//创建黑色边框
if($_flag)
{
$_black=imagecolorallocate($image,0,0,0);
imagerectangle($image,0,0,$width-1,$height-1,$_black);
} //随机画6个线条
for($i=0;$i<6;$i++)
{
$_line_color=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$_line_color);
}
//随机雪花
for($i=0;$i<100;$i++)
{
$_snow_color=imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($image,1,mt_rand(1,$width),mt_rand(1,$height),'*',$_snow_color);
}
//输出验证码
for($i=0;$i<strlen($_SESSION['code']);$i++)
{
$_out_color=imagecolorallocate($image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($image,5,$width*$i/$_rnd_code+mt_rand(0,10),mt_rand(1,$height/2),$_SESSION['code'][$i],$_out_color);
}//输出图像
header("Content-Type:image/png");
imagepng($image);
//销毁
imagedestroy($image);
}----------------------------------产生错误如下-----------------------------------------------------图像 “http://localhost/GuestTest08/code.php “因其本身有错误无法显示
session_start();
//定义个常量,用来授权调用includes里面的文件
define('IN_TG',true);
//引入公共文件
require dirname(__FILE__).'/includes/common.inc.php'; //此文件中引用了 global.func.php 文件
//调用方法生成验证码
_code();-----------------------------以下是global.func.php文件-------------------------------------------------------- /**
* _code()是验证码函数
* @access public
* @param int $width 表示验证码的长度
* @param int $height 表示验证码的高度
* @param int $_rnd_code 表示验证码的位数
* @param bool $_flag 表示验证码是否需要边框
* @return void 这个函数执行后产生一个验证码
*/
function _code($width=75,$height=25,$_rnd_code=4,$_flag=false)
{
//随机码的个数
$_rnd_code=4;
//随机产生验证码
$_nmsg="";
for($i=0;$i<$_rnd_code;$i++)
{
$_nmsg .=dechex(mt_rand(0,15));
}
//将验证码保存在session中
$_SESSION['code']=$_nmsg;
//创建一张图像
$image=imagecreatetruecolor($width,$height);//背景颜色
$_white=imagecolorallocate($image,255,255,255);
//填充颜色
imagefill($image,0,0,$_white);
//创建黑色边框
if($_flag)
{
$_black=imagecolorallocate($image,0,0,0);
imagerectangle($image,0,0,$width-1,$height-1,$_black);
} //随机画6个线条
for($i=0;$i<6;$i++)
{
$_line_color=imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$_line_color);
}
//随机雪花
for($i=0;$i<100;$i++)
{
$_snow_color=imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($image,1,mt_rand(1,$width),mt_rand(1,$height),'*',$_snow_color);
}
//输出验证码
for($i=0;$i<strlen($_SESSION['code']);$i++)
{
$_out_color=imagecolorallocate($image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($image,5,$width*$i/$_rnd_code+mt_rand(0,10),mt_rand(1,$height/2),$_SESSION['code'][$i],$_out_color);
}//输出图像
header("Content-Type:image/png");
imagepng($image);
//销毁
imagedestroy($image);
}----------------------------------产生错误如下-----------------------------------------------------图像 “http://localhost/GuestTest08/code.php “因其本身有错误无法显示
require dirname(__FILE__).'/include/common.inc.php';//其他一些不确定是否有输出的语句放这里ob_end_clean();//清空缓存_code();