[笔记]简单的验证码制作

来源:互联网 发布:达观数据 融资 编辑:程序博客网 时间:2024/05/16 00:26

简单的验证码制作

html

<?php if (isset($_REQUEST['authcode'])){    session_start();    if (strtolower($_REQUEST['authcode'])==$_SESSION['authcode']){        echo '<font color=#0000CC>输入正确</font>';    }else {        echo '<font color=#0000CC>输入错误</font>';    }    exit();}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//CN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>管理员登录 -- 企业网站后台管理系统</title><title>Insert title here</title></head>    <body>        <form action="./yanz.php" method="post">            <p>验证图片:<img border="1" src="./ceshi.php?r=<?php echo rand();?>"width="100" height="30"></p>            <p>请输入验证内容:<input type="text"name="authcode" vlane=""></p>            <p><input type="submit" value="提交" style="padding: 6px 20px;"></p>        </form>    </body></html>

PHP–数字,英文

<?php   //创建gd画布   session_start();   $width=100;   $height=30;   $image=imagecreatetruecolor($width, $height);   $bgcolor = imagecolorallocate( $image, 255, 255, 255 );   imagefill( $image, 0, 0, $bgcolor );   /*for ($i=0;$i<4;$i++){       $fontsize=6;       $fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120),rand(0,120));       $fontcontent=rand(0,9);       $x=($i*100/4)+rand(5,10);       $y=rand(5,10);       imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);   }*/   $captch_code='';   for ($i=0;$i<4;$i++){       $fontsize=6;       $fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120),rand(0,120));       $data='abcdefghijklmnopqrtuvwxyz123456789';       $fontcontent=substr($data, rand(0,strlen($data)),1);       $captch_code=$fontcontent;       $x=($i*100/4)+rand(5,10);       $y=rand(5,10);       imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);   }   $_SESSION['authcode']=$captch_code;   //干扰元素之干扰点   for ($i=0;$i<200;$i++){       $pointcolor=imagecolorallocate($image, rand(0,120),rand(0,120),rand(0,120));       imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);   }   //干扰元素之干扰线   for ($i=0;$i<3;$i++){       $linecolor=imagecolorallocate($image, rand(80,120),rand(80,120),rand(80,120));       imageline($image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), $linecolor);   }   header('content-type:image/png');   imagepng($image);   imagedestroy($image);

PHP–图片

<?php   //创建gd画布   session_start();   $table=arraay(       'pic0'=>'狗',       'pic1'=>'猫',       'pic2'=>'鱼',       'pic3'=>'鸟',       );   $index=rand(0,3);   $value=$table['pic'.$index];   $_SESSION['authcode']=$value;   $filename=dirname(_FILE_).'\\pic'.$index.'jpg';   $content=file_get_contents($filename);   header('content-type:image/jpg');   echo $content;

PHP–文字

<?php   //创建gd画布   session_start();   $width=200;   $height=60;   $image=imagecreatetruecolor($width, $height);   $bgcolor = imagecolorallocate( $image, 255, 255, 255 );   imagefill( $image, 0, 0, $bgcolor );   /*for ($i=0;$i<4;$i++){       $fontsize=6;       $fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120),rand(0,120));       $fontcontent=rand(0,9);       $x=($i*100/4)+rand(5,10);       $y=rand(5,10);       imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);   }*/   $fontface='SIMYOU.TTF';   $str='汉字'   $strdb_code='';   //$strdb=array('我','是','谁','呢');   $strdb=str_split($str,3);   $captch_code='';   for ($i=0;$i<4;$i++){       //$fontsize=6;       $fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120),rand(0,120));       //$cn=$strdb[$i];       $index=rand(0,count($strdb));       $cn=$strdb[$index];       $captch_code.=$cn;       imagettftext($image, mt_rand(20,40),mt_rand(-60 ,60), (40*$i+20), mt_rand(30,35), $fontcolor, $fontfile, $fontface,$cn);   }   $_SESSION['authcode']=$captch_code;   //干扰元素之干扰点   for ($i=0;$i<200;$i++){       $pointcolor=imagecolorallocate($image, rand(50,200),rand(50,200),rand(50,200));       imagesetpixel($image, rand(1,299), rand(1,59), $pointcolor);   }   //干扰元素之干扰线   for ($i=0;$i<3;$i++){       $linecolor=imagecolorallocate($image, rand(80,120),rand(80,120),rand(80,120));       imageline($image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), $linecolor);   }   header('content-type:image/png');   imagepng($image);   imagedestroy($image);
原创粉丝点击