Home >php教程 >php手册 >php生成验证码详细教程

php生成验证码详细教程

WBOY
WBOYOriginal
2016-06-13 09:48:33933browse

本文章提供一款php教程生成验证码详细教程,前面是讲关于生成验证核心代码,后面是一款生成与调用的方法。

//header("content-type:image/png");
$num ='1234';
$imagewidth=60;
$imageheight=18;

$numimage = imagecreate($imagewidth,$imageheight);
imagecolorallocate($numimage,240,240,240);
for($i=0;$i $x = mt_rand(1,8)+$imagewidth*$i/4;
$y = mt_rand(1,$imageheight/4);
$color=imagecolorallocate($numimage,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
imagestring($numimage,5,$x,$y,$num[$i],$color);
}

for($i=0;$i $randcolor=imagecolorallocate($numimage,rand(200,255),rand(200,255),rand(200,255));
imagesetpixel($numimage,rand()%70,rand()%20,$randcolor);
}
imagepng($numimage);
imagedestroy($numimage);
?>


来看一款生成验证码实例

   //生成验证码图片

    session_start();

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

  srand((double)microtime()*1000000); 

  $roundnum=rand(1000,9999);

  //把随机数存入session以便以后用

   $_session["sessionround"]=$roundnum;

        $im = imagecreate(58,28);

        $red = imagecolorallocate($im, 255,0,0);

        $blue = imagecolorallocate($im, 0,255,0);

 //局域填充,相当于背景

        imagefill($im,68,30,$red);

   //将四位整数验证码绘入图片

        imagestring($im, 5, 10, 8, $roundnum, $blue);

        for($i=0;$i

        {

                imagesetpixel($im, rand()%70 , rand()%30 , $black);

        }

        imagepng($im);

        imagedestroy($im);

?>


html调用 方法

验证码

    

     php生成验证码详细教程

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn