欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于php验证码函数的使用示例

程序员文章站 2023-09-09 14:37:24
复制代码 代码如下:function _code($_code_length = 4, $_width = 75, $_height = 25){  &...

复制代码 代码如下:

function _code($_code_length = 4, $_width = 75, $_height = 25){
    for($i=0;$i<$_code_length;$i++){
        $_nmsg .= dechex(mt_rand(0,15));
    }
    $_session["code"] = $_nmsg;

    $_img = imagecreatetruecolor($_width, $_height);

    $_white = imagecolorallocate($_img, 250, 250, 250);

    imagefill($_img, 0, 0, $_white);

    $_gray = imagecolorallocate($_img, 196, 196, 196);

    imagerectangle($_img, 0, 0, $_width-1, $_height-1, $_gray);

    for ($i=0; $i < 6; $i++) {
        $_md_color = imagecolorallocate($_img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
        imageline($_img, mt_rand(0,$_width), mt_rand(0, $_height),mt_rand(0,$_width), mt_rand(0, $_height), $_md_color);
    }

    for ($i=0; $i < 50; $i++) {
        $_md_color = imagecolorallocate($_img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
        imagestring($_img, 1, mt_rand(1,$_width-5), mt_rand(1, $_height-5), "*", $_md_color);
    }

    for ($i=0; $i < $_code_length ; $i++) {
        $_md_color = imagecolorallocate($_img, mt_rand(0,102), mt_rand(0,102), mt_rand(0,102));
        imagestring($_img, 5, $i * $_width/$_code_length+ mt_rand(1, 10), mt_rand(1, $_height/2), $_session["code"][$i], $_md_color);
    }

    header("content-type:image/png");

    imagepng($_img);

    imagedestroy($_img);
}