在php中要生成验证码图片是相当的简单的,因为在php中为我们提供了图形gd.dll库,要启用gd图形库我们只要在在php.ini中把php-gd前面的;去就可以了。
方法一,代码如下:
- $authnum='';
- $ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
- $list=explode(",",$ychar);
- for($i=0;$i<4;$i++){
- $randnum=rand(0,35);
- $authnum.=$list[$randnum];
方法二,代码如下:
- private function createCheckCode()
- {
- for(i=0;i<this->codeNum;i++)
- {
- number = rand(0,2);
- switch(number)
- {
- case 0: rand_number = rand(48,57); break;
- case 1: rand_number = rand(65,90);break;
- case 2: rand_number = rand(97,122);break;
- }
- $asc = sprintf("%c",rand_number);
- $asc_number = asc_number.asc;
- }
- return asc_number;
- }
方法三,代码如下:
- srand(microtime()*100000);
- $string="abcdefghigklmnopqrstuvwxyz123456789";
- for($i=0;$i<4;$i++)
- {
- $new_number.=$string[rand(0,strlen($string)-1)];
- }
方法四,代码如下:
- for($i=0;$i<4;$i++)
- {
- $rand.=dechex(rand(1,15));
- }
随机生成数字,字母的代码:
- <?php
-
- session_start();
- for($i=0;$i<4;$i++)
- {
- $rand.=dechex(rand(1,15));
- }
- $_SESSION['check_num']=$rand;
- $image=imagecreatetruecolor(50,30);
- $bg=imagecolorallocate($im,0,0,0);
- $te=imagecolorallocate($im,255,255,255);
- imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
- ob_clean();
- header("Content-type:image/jpeg"); imagejpeg($image);
- ?>
给图片画出干扰线代码:
- for($i=0;$i<8;$i++)
- {
- $cg=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
- imageline($im,rand(10,40),0,rand(10,40),20,$cg);
- }
给图片画出干扰点的代码:
- for($i=0;$i<80;$i++)
- {
- imagesetpixel($im,rand(0,40),rand(0,20),$cg);
- }
把文字写入图片代码:
- $str=array('我','我','亲','亲');
- for($i=0;$i<4;$i++)
- {
- $sss.=$str[rand(0,3)];
- }
-
- imagettftext($im,10,0,rand(5,60),rand(5,60),$te,"simhei.ttf",$sss);
最后我们结合实际分享一个完整的实例,代码如下:
-
-
-
-
-
- function valiCode($word='randcode'){
- Header("Content-type: image/gif");
- $border = 0;
- $how = 4;
- $w = $how*15;
- $h = 18;
- $fontsize = 10;
- $alpha = "abcdefghijkmnpqrstuvwxyz";
- $number = "23456789";
- $randcode = "";
- srand((double)microtime()*1000000);
- $im = ImageCreate($w, $h);
-
-
-
- $bgcolor = ImageColorAllocate($im, 255, 255, 255);
- ImageFill($im, 0, 0, $bgcolor);
- if($border)
- {
- $black = ImageColorAllocate($im, 0, 0, 0);
- ImageRectangle($im, 0, 0, $w-1, $h-1, $black);
- }
-
-
-
-
- for($i=0; $i<$how; $i++)
- {
- $alpha_or_number = mt_rand(0, 1);
- $str = $alpha_or_number ? $alpha : $number;
- $which = mt_rand(0, strlen($str)-1);
- $code = substr($str, $which, 1);
- $j = !$i ? 4 : $j+15;
- $color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
- ImageChar($im, $fontsize, $j, 3, $code, $color3);
- $randcode .= $code;
- }
-
-
-
-
-
-
-
- for($i=0; $i<5; $i++)
- {
- $color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
- ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1);
- }
-
-
-
-
-
-
-
-
-
- for($i=0; $i<$how*40; $i++)
- {
- $color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
- ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2);
- }
-
-
-
-
- $_SESSION[$word] = $randcode;
-
- Imagegif($im);
- ImageDestroy($im);
-
- }
- 调用方法也很简单把上面实例保存img.php文件,然后在要调用的页面
- html页面如下
- 代码如下 复制代码
- <script language="javascript">
- function refresh_code()
- {
- form1.imgcode.src="verifycode.php?a="+Math.random();
- }
- </script>
- <form id="form1" name="form1" method="post" action="checkcode.php">
- <label for="code">验证码:</label>
- <input type="text" name="code" id="textfield" />
- <img id="imgcode" src="VerifyCode.php" alt="验证码" />
- <a href="javascript:refresh_code()">看不清?换一个</a>
- <input type="submit" name="button" id="button" value="提交" />
- </form>
这要就可以实现验证码调用了,再加个提交验证验证码是否正确,代码如下:
- <?php
- session_start();
- if((strtoupper($_POST["code"])) == strtoupper(($_SESSION["VerifyCode"]))){
- print("验证码正确,");
- }else{
- print("验证码错误,");
- }
- ?>
这要就我们完成了从生成验证码图片和使用的整个过程了,也算是从php入门到精通验证码全部讲了。 |