急急!关于验证码的问题!
时间:2011-12-24
来源:互联网
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 “因其本身有错误无法显示
<?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 “因其本身有错误无法显示
作者: rzwd1 发布时间: 2011-12-24
引用这个验证码的页面可能进行写操作了, 问题应该出现在这里header("Content-Type:image/png"),这之前是不许有写操作的,而你的code.php里面没有echo这类的东西,我猜可能是引用code.php这个的页面有页面写操作吧。建议不直接输出,而是生成随机的图片文件,用img的src调用也可以
作者: hellodifa 发布时间: 2011-12-24
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28