本文章提供一款php生成验证码详细教程,前面是讲关于生成验证核心代码,后面是一款生成与调用的方法,php生成验证码实例代码如下:
- <?php
-
- $num ='1234';
- $imagewidth=60;
- $imageheight=18;
-
- $numimage = imagecreate($imagewidth,$imageheight);
- imagecolorallocate($numimage,240,240,240);
- for($i=0;$i<strlen($num);$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<200;$i++){
- $randcolor=imagecolorallocate($numimage,rand(200,255),rand(200,255),rand(200,255));
- imagesetpixel($numimage,rand()%70,rand()%20,$randcolor);
- }
- imagepng($numimage);
- imagedestroy($numimage);
- ?>
来看一款生成验证码实例,代码如下:
- <?php
-
-
-
- session_start();
-
- header("content-type: image/png");
-
- srand((double)microtime()*1000000);
-
- $roundnum=rand(1000,9999);
-
-
-
- $_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<50;$i++)
-
- {
-
- imagesetpixel($im, rand()%70 , rand()%30 , $black);
-
- }
-
- imagepng($im);
-
- imagedestroy($im);
-
- ?>
html调用方法,代码如下:
- <td>验证码</td><td width="20%">
- <input name="validt" type="text" size="10" maxlength="4"/></td><td colspan="3" width="80%">
- <img src="/www.phpfensi.com/validitpicture.php" name="validitpic" align="absmiddle" /></td>
|