Пример #1
0
* 最后版本:$LastChangedRevision: 626 $                                 
* 修 改 者:$LastChangedBy: tony_ren $                                      
* 版本地址:$HeadURL: http://192.168.1.100:12000/svn/rocktrain/trunk/RockAdmin/public/psys/v.php $                                            
* 摘    要:验证码                                                       
*/
$pubcomm = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . "protected" . DIRECTORY_SEPARATOR . "publib" . DIRECTORY_SEPARATOR . "comm" . DIRECTORY_SEPARATOR;
require_once $pubcomm . 'XSession.php';
//随机生成一个4位数的数字验证码
$num = "";
for ($i = 0; $i < 4; $i++) {
    $num .= rand(0, 9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证页面使用
//Session_start();
XSession::Init();
XSession::Set("avacode", $num);
//创建图片,定义颜色值
//Header("Content-type: image/PNG");
srand((double) microtime() * 1000000);
$im = imagecreatetruecolor(60, 20);
$black = ImageColorAllocate($im, 0, 0, 0);
$gray = ImageColorAllocate($im, 200, 200, 200);
imagefill($im, 0, 0, $gray);
//随机绘制两条虚线,起干扰作用
$style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);
imagesetstyle($im, $style);
$y1 = rand(0, 20);
$y2 = rand(0, 20);
$y3 = rand(0, 20);
$y4 = rand(0, 20);