PHP实现登录图形验证码(gd)
- 代码如下:
<?php
function GetRandStr($length){
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$len = strlen($str)-1;
$randstr = '';
for($i=0;$i<$length;$i++) {
$num=mt_rand(0,$len);
$randstr .=" ".$str[$num];
}
return $randstr;
}
$code = GetRandStr(4);
session_start();
$_SESSION['code'] = str_replace(" ", "", $code);
$im = imageCreateTrueColor(100, 50);
$white = imageColorAllocate($im, 255, 255, 255);
$black = imageColorAllocate($im, 0, 0, 64);
imageFill($im, 0, 0, $white);
imageLine($im, 0, rand(20,50), 200, rand(20,50), $black);
imageString($im, 12, 6, 20, $code, $black);
header('Content-type: image/png');
imagePng($im);
imageDestroy($im);
?>
- 生成效果展示:
|