关于验证码的问题!无法生成图片,显示乱码。

awincsdn 2007-02-14 02:49:17
==============hcms.class.php============
<?php
class Hcms{
private $seccode ;
/*
*/
function __construct(){
//if(!$nocacheheaders) {
/*
header("Expires: -1");
header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
header("Pragma: no-cache");
header("Content-Type: image/bmp;");
*/
//}

$this->seccode = sprintf('%04d',$this->random(4,1));

}
function doit(){
//
$numbers = array
(
0 => array('3c','66','66','66','66','66','66','66','66','3c'),
1 => array('1c','0c','0c','0c','0c','0c','0c','0c','1c','0c'),
2 => array('7e','60','60','30','18','0c','06','06','66','3c'),
3 => array('3c','66','06','06','06','1c','06','06','66','3c'),
4 => array('1e','0c','7e','4c','2c','2c','1c','1c','0c','0c'),
5 => array('3c','66','06','06','06','7c','60','60','60','7e'),
6 => array('3c','66','66','66','66','7c','60','60','30','1c'),
7 => array('30','30','18','18','0c','0c','06','06','66','7e'),
8 => array('3c','66','66','66','66','3c','66','66','66','3c'),
9 => array('38','0c','06','06','3e','66','66','66','66','3c')
);

//
for($i = 0; $i < 10; $i++) {
for($j = 0; $j < 6; $j++) {
$a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);
$a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1);
mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);
mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);
}
}
//
$bitmap = array();
for($i = 0; $i < 20; $i++) {
for($j = 0; $j < 4; $j++) {
$n = substr($this->seccode, $j, 1);
$bytes = $numbers[$n][$i];
$a = mt_rand(0, 14);
switch($a) {
case 1: str_replace('9', '8', $bytes); break;
case 3: str_replace('c', 'e', $bytes); break;
case 6: str_replace('3', 'b', $bytes); break;
case 8: str_replace('8', '9', $bytes); break;
case 0: str_replace('e', 'f', $bytes); break;
}
array_push($bitmap, $bytes);
}
}
//
for($i = 0; $i < 8; $i++) {
$a = substr('012', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);
array_unshift($bitmap, $a);
array_push($bitmap, $a);
}

$image = pack('H*', '424d9e000000000000003e000000280000002000000018000000010001000000'.
'0000600000000000000000000000000000000000000000000000FFFFFF00'.implode('', $bitmap));

return $image;
}
/*
*/
public function __destruct(){
$this->seccode = null;
}
/**/
function getSeccode(){
return $this->seccode;
}

/**/
function random($length, $numeric = 0){
mt_srand((double)microtime() * 1000000);
if($numeric) {
$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}
}

?>
==========================noname1.php============================
<?php
require_once("hcms.class.php");
$s = new Hcms();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Content-Type" content="image/bmp; charset=iso-8859-1">
<title>Untitled</title>
<link rel="stylesheet" type="text/css" href="my.css">
</head>
<body>
<?php
//require_once("hcms.class.php");
// $s = new Hcms();
print $s->doit();
?>
</body>
</html>
=====================乱码如下======================
BMž>( `ÿÿÿ#!C!!#!21#!Cf|ff`ff`f<0ff<<!#!#!$!#

高手请帮忙解决!
...全文
929 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
awincsdn 2007-02-14
  • 打赏
  • 举报
回复
十分感谢,由你一讲,我也突然明白了其中的道理。我悬入了header这个陷阱里。没有好好了理解其意义。
cymple 2007-02-14
  • 打赏
  • 举报
回复
LZ,你不能直接调用这个函数产生的图片
假如单纯的
noname1.php
<?php
$s= new Hcms();
echo $s->doit();
?>
这样是对的
你应该把一个图片的插入
noname1.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Content-Type" content="image/bmp; charset=iso-8859-1">
<title>Untitled</title>
<link rel="stylesheet" type="text/css" href="my.css">
</head>
<body>
<img src='img.php'>
</body>
</html>
img.php:
<?php
require 'hcms.class.php';
$s= new Hcms();
echo $s->doit();
?>

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧