解析dedeCMS验证码的实现代码
程序员文章站
2022-12-27 08:22:54
如下所示:复制代码 代码如下:
如下所示:
<?php
$rndstring = '';
for($i=0; $i<4; $i++) $rndstring .= chr(mt_rand(65,90));
//如果支持gd,则绘图
if(function_exists("imagecreate"))
{
//firefox部份情况会多次请求的问题,5秒内刷新页面将不改变session
$ntime = time();
if(empty($_session['dd_ckstr_last']) || empty($_session['dd_ckstr']) || ($ntime - $_session['dd_ckstr_last'] > 5))
{
$_session['dd_ckstr'] = strtolower($rndstring);
$_session['dd_ckstr_last'] = $ntime;
}
$rndstring = $_session['dd_ckstr'];
$rndcodelen = strlen($rndstring);
//创建图片,并设置背景色
$im = imagecreate(50,20);
imagecolorallocate($im, 255,255,255);
//背景线
$linecolor1 = imagecolorallocate($im,240,220,180);
$linecolor2 = imagecolorallocate($im,250,250,170);
for($j=3;$j<=16;$j=$j+3)
{
imageline($im,2,$j,48,$j,$linecolor1);
}
for($j=2;$j<52;$j=$j+(mt_rand(3,6)))
{
imageline($im,$j,2,$j-6,18,$linecolor2);
}
//画边框
$bordercolor = imagecolorallocate($im, 0x99,0x99,0x99);
imagerectangle($im, 0, 0, 49, 19, $bordercolor);
//输出文字
$fontcolor = imagecolorallocate($im, 48,61,50);
for($i=0;$i<$rndcodelen;$i++)
{
$bc = mt_rand(0,1);
$rndstring[$i] = strtoupper($rndstring[$i]);
imagestring($im, 5, $i*10+6, mt_rand(2,4), $rndstring[$i], $fontcolor);
}
header("pragma:no-cache/r/n");
header("cache-control:no-cache/r/n");
header("expires:0/r/n");
//输出特定类型的图片格式,优先级为 gif -> jpg ->png
if(function_exists("imagejpeg"))
{
header("content-type:image/jpeg/r/n");
imagejpeg($im);
}
else
{
header("content-type:image/png/r/n");
imagepng($im);
}
imagedestroy($im);
exit();
}
else
{
//不支持gd,只输出字母 abcd
$_session['dd_ckstr'] = "abcd";
$_session['dd_ckstr_last'] = '';
header("content-type:image/jpeg/r/n");
header("pragma:no-cache/r/n");
header("cache-control:no-cache/r/n");
header("expires:0/r/n");
$fp = fopen("data/vdcode.jpg","r");
echo fread($fp,filesize("data/vdcode.jpg"));
fclose($fp);
exit();
}
?>
复制代码 代码如下:
<?php
$rndstring = '';
for($i=0; $i<4; $i++) $rndstring .= chr(mt_rand(65,90));
//如果支持gd,则绘图
if(function_exists("imagecreate"))
{
//firefox部份情况会多次请求的问题,5秒内刷新页面将不改变session
$ntime = time();
if(empty($_session['dd_ckstr_last']) || empty($_session['dd_ckstr']) || ($ntime - $_session['dd_ckstr_last'] > 5))
{
$_session['dd_ckstr'] = strtolower($rndstring);
$_session['dd_ckstr_last'] = $ntime;
}
$rndstring = $_session['dd_ckstr'];
$rndcodelen = strlen($rndstring);
//创建图片,并设置背景色
$im = imagecreate(50,20);
imagecolorallocate($im, 255,255,255);
//背景线
$linecolor1 = imagecolorallocate($im,240,220,180);
$linecolor2 = imagecolorallocate($im,250,250,170);
for($j=3;$j<=16;$j=$j+3)
{
imageline($im,2,$j,48,$j,$linecolor1);
}
for($j=2;$j<52;$j=$j+(mt_rand(3,6)))
{
imageline($im,$j,2,$j-6,18,$linecolor2);
}
//画边框
$bordercolor = imagecolorallocate($im, 0x99,0x99,0x99);
imagerectangle($im, 0, 0, 49, 19, $bordercolor);
//输出文字
$fontcolor = imagecolorallocate($im, 48,61,50);
for($i=0;$i<$rndcodelen;$i++)
{
$bc = mt_rand(0,1);
$rndstring[$i] = strtoupper($rndstring[$i]);
imagestring($im, 5, $i*10+6, mt_rand(2,4), $rndstring[$i], $fontcolor);
}
header("pragma:no-cache/r/n");
header("cache-control:no-cache/r/n");
header("expires:0/r/n");
//输出特定类型的图片格式,优先级为 gif -> jpg ->png
if(function_exists("imagejpeg"))
{
header("content-type:image/jpeg/r/n");
imagejpeg($im);
}
else
{
header("content-type:image/png/r/n");
imagepng($im);
}
imagedestroy($im);
exit();
}
else
{
//不支持gd,只输出字母 abcd
$_session['dd_ckstr'] = "abcd";
$_session['dd_ckstr_last'] = '';
header("content-type:image/jpeg/r/n");
header("pragma:no-cache/r/n");
header("cache-control:no-cache/r/n");
header("expires:0/r/n");
$fp = fopen("data/vdcode.jpg","r");
echo fread($fp,filesize("data/vdcode.jpg"));
fclose($fp);
exit();
}
?>
下一篇: php之XML转数组函数的详解