参考下:
<?php
define('THIS_SCRIPT', 'getcode');
@header("Expires: -1");
@header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
@header("Pragma: no-cache");
session_name('GKQW_ID');
session_start();
$seccode = random(4,1);
$seccode = sprintf('%04d', $seccode);
$_SESSION['seccode']=$seccode;
//检测是否打开GD库
$gd = 1;//1使用GD库0不使用
if($gd && function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagesetpixel') && function_exists('imagechar') && function_exists('imagepng') && function_exists('imagettftext')) {
$width = "65";//图片宽
$height = "20";//图片高
$len = "4";//生成几位验证码
$bgcolor = "#ffffff";//背景色
$noise = true;//生成杂点
$noisenum = 150;//杂点数量
$border = false;//边框
$bordercolor = "#000000";
$image = imageCreate($width, $height);
$back = getcolor($bgcolor);
$code='';
imageFilledRectangle($image, 0, 0, $width, $height, $back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
for ($i=0; $i<$len; $i++)
{
$randtext = $seccode[$i];
$code .= $randtext;
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
$rand = rand(1,4);
if ($rand < 1 OR $rand > 4) {
$rand = 1;
}
$font = realpath("getcode/".$rand.".ttf");
$randsize = rand($size-$size/10, $size+$size/10);
$location = $left+($i*$size+$size/10);
@imagettftext($image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textColor, $font, $randtext);
}
if($noise == true) setnoise();
//$_SESSION['code'] = $code;
$bordercolor = getcolor($bordercolor);
if($border==true) imageRectangle($image, 0, 0, $width-1, $height-1, $bordercolor);
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);
}else{ $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($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)); header('Content-Type: image/bmp');
echo $image;
echo $seccode;
}function getcolor($color)
{
global $image;
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($image, $r, $b, $g);
return $color;
}
function setnoise()
{
global $image, $width, $height, $back, $noisenum;
for ($i=0; $i<$noisenum; $i++){
$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
}
}
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;
}
?>
<?php
define('THIS_SCRIPT', 'getcode');
@header("Expires: -1");
@header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
@header("Pragma: no-cache");
session_name('GKQW_ID');
session_start();
$seccode = random(4,1);
$seccode = sprintf('%04d', $seccode);
$_SESSION['seccode']=$seccode;
//检测是否打开GD库
$gd = 1;//1使用GD库0不使用
if($gd && function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagesetpixel') && function_exists('imagechar') && function_exists('imagepng') && function_exists('imagettftext')) {
$width = "65";//图片宽
$height = "20";//图片高
$len = "4";//生成几位验证码
$bgcolor = "#ffffff";//背景色
$noise = true;//生成杂点
$noisenum = 150;//杂点数量
$border = false;//边框
$bordercolor = "#000000";
$image = imageCreate($width, $height);
$back = getcolor($bgcolor);
$code='';
imageFilledRectangle($image, 0, 0, $width, $height, $back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
for ($i=0; $i<$len; $i++)
{
$randtext = $seccode[$i];
$code .= $randtext;
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
$rand = rand(1,4);
if ($rand < 1 OR $rand > 4) {
$rand = 1;
}
$font = realpath("getcode/".$rand.".ttf");
$randsize = rand($size-$size/10, $size+$size/10);
$location = $left+($i*$size+$size/10);
@imagettftext($image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textColor, $font, $randtext);
}
if($noise == true) setnoise();
//$_SESSION['code'] = $code;
$bordercolor = getcolor($bordercolor);
if($border==true) imageRectangle($image, 0, 0, $width-1, $height-1, $bordercolor);
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);
}else{ $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($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)); header('Content-Type: image/bmp');
echo $image;
echo $seccode;
}function getcolor($color)
{
global $image;
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($image, $r, $b, $g);
return $color;
}
function setnoise()
{
global $image, $width, $height, $back, $noisenum;
for ($i=0; $i<$noisenum; $i++){
$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
}
}
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;
}
?>
<?
//checkNum.php Function getRandNumber ($fMin, $fMax) {
srand((double)microtime()*1000000);
$fLen = "%0 ".strlen($fMax). "d ";
Return sprintf($fLen, rand($fMin,$fMax));
}
$str=getRandNumber(1000,9999);
setcookie("code",$str);//function images($str){
//$str=random(4); //随机生成的字符串 $width = 50; //验证码图片的宽度
$height = 22; //验证码图片的高度
@header("Content-Type:image/png");
//$_SESSION["code"] = $str;
//echo $str;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊点颜色
$pix=imagecolorallocate($im,187,230,247);
//字体色
$font=imagecolorallocate($im,41,163,238);
//绘模糊作用的点
mt_srand();
for($i=0;$i<1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 3,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
//$_SESSION["code"] = $str;//}?>
小弟PHP白
<?php
//checkNum.php Function getRandNumber ($fMin, $fMax) {
srand((double)microtime()*1000000);
$fLen = "%0 ".strlen($fMax). "d ";
Return sprintf($fLen, rand($fMin,$fMax));
}
$str=getRandNumber(1000,9999);
setcookie("code",$str);
//function images($str){
//$str=random(4); //随机生成的字符串 $width = 50; //验证码图片的宽度
$height = 22; //验证码图片的高度
@header("Content-Type:image/png");
//$_SESSION["code"] = $str;
//echo $str;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊点颜色
$pix=imagecolorallocate($im,187,230,247);
//字体色
$font=imagecolorallocate($im,41,163,238);
//绘模糊作用的点
mt_srand();
for($i=0;$i <1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 3,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
//$_SESSION["code"] = $str;//}?>
不过建议3#,最好写成
<?php
....
?>
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.1.9
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled
以上是我这里PHPINFO()中的GD信息,查看你自已的是否开启
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
*
* @version $Id: phpinfo.php 10240 2007-04-01 11:02:46Z cybot_tm $
*//**
* Gets core libraries and defines some variables
*/
define('PMA_MINIMUM_COMMON', true);
require_once './libraries/common.inc.php';
/**
* Displays PHP information
*/
if ($GLOBALS['cfg']['ShowPhpInfo']) {
phpinfo();
}
?>我的是这个,.
但我是在服务器上做的.所以本机的这个没有关系的吧
\
是index还是image呢?
<?
//checkNum.php Function getRandNumber ($fMin, $fMax) {
srand((double)microtime()*1000000);
$fLen = "%0 ".strlen($fMax). "d ";
Return sprintf($fLen, rand($fMin,$fMax));
}
$str=getRandNumber(1000,9999);
setcookie("code",$str);
//function images($str){
//$str=random(4); //随机生成的字符串 $width = 50; //验证码图片的宽度
$height = 22; //验证码图片的高度
@header("Content-Type:image/png");
//$_SESSION["code"] = $str;
//echo $str;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
//模糊点颜色
$pix=imagecolorallocate($im,187,230,247);
//字体色
$font=imagecolorallocate($im,41,163,238);
//绘模糊作用的点
mt_srand();
for($i=0;$i <1000;$i++)
{
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($im, 5, 7, 3,$str, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
imagepng($im);
imagedestroy($im);
//session_start();
//$_SESSION["code"] = $str; //} ?> 在需要使用验证码的页面调用,一般放在表单后面
<input name="chknumber" type="text" id="textfield" size="5" maxlength="4" />
<img src="../chknumber.php" />
提交后数据处理页面吧post过来的用户输入的验证码跟cookie里的验证码比对,该怎么办就怎么办
$chknumber=$_POST['chknumber'];
if ( intval($chknumber)!=intval($_COOKIE['code']))
对于支持session的可以使用session
吧最上面那段代码
下面//session_start();
//$_SESSION["code"] = $str; 的注释去掉
同样吧上面setcookie()那个注释了,处理页面
$chknumber=$_POST['chknumber'];
session_start();
if ( intval($chknumber)!=intval($_SESSION['code']))