验证码

以下代码直接复制粘贴即可得到验证码

<?php
// 验证码
@session_start();
$_SESSION["randcode"] = null;
$type = 'png';
$width = 50;
$height = 20;
$randval = randStr(4, "");
$_SESSION['randcode'] = $randval;
//print_r($_SESSION);exit;
header("Content-type: image/" . $type);
srand((double)microtime() * 1000000);

if ($type != 'png' && function_exists('imagecreatetruecolor')) {
    $im = @imagecreatetruecolor($width, $height);
} else {
    $im = @imagecreate($width, $height);
}
$r = Array(210, 50, 120);
$g = Array(240, 225, 235);
$b = Array(250, 225, 10);

$rr = Array(255, 240, 0);
$gg = Array(100, 0, 0);
$bb = Array(0, 0, 205);
$key = rand(0, 2);
$stringColor = ImageColorAllocate($im, 255, 255, 255); //字体颜色
$backColor = ImageColorAllocate($im, 18, 121, 226);//背景色(随机)
$borderColor = ImageColorAllocate($im, 16, 140, 232);//边框色
$pointColor = ImageColorAllocate($im, 18, 121, 226);//点颜色

@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor); //边框位置
@imagestring($im, 5, 7, 3, $randval, $stringColor); //调整字型位置
$ImageFun = 'Image' . $type;
$ImageFun($im);
@ImageDestroy($im);

//产生随机字符串
function randStr($len = 6, $format = 'ALL')
{
    switch ($format) {
        default :
            $chars = '0123456789';
            break;
    }
    $string = "";
    while (strlen($string) < $len)
        $string .= substr($chars, (mt_rand() % strlen($chars)), 1);
    return $string;
}

?>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容