PHP 开源:dedeCMS 验证码的实现代码
本文为大家重点介绍 dedeCMS 验证码的实现代码,欢迎参考下
如下所示:
代码如下:
<?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
);