加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 学习资料

验证码

时间:2021-03-21 21:54:58  来源:  作者:
index.php
  1. <?php
  2. session_start();
  3. // 丢弃输出缓冲区的内容 **
  4. ob_clean();
  5. header ('Content-Type: image/png');
  6. // 1 创建画布
  7. $image=imagecreatetruecolor(100, 30);
  8. // 2 画一个矩形并填充背景颜色为白色
  9. $color=imagecolorallocate($image, 255, 255, 255);
  10. imagefill($image, 20, 20, $color);
  11. // for($i=0;$i<4;$i++){
  12.     // $font=6;
  13.     // $x=rand(5,10)+$i*100/4;
  14.     // $y=rand(8, 15);
  15.     // $string=rand(0, 9);
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
  17.     // imagestring($image, $font, $x, $y, $string, $color);
  18. // }
  19. $code='';
  20. for($i=0;$i<4;$i++){
  21.     $fontSize=8;
  22.     $x=rand(5,10)+$i*100/4;
  23.     $y=rand(5, 15);
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
  25.     $string=substr($data,rand(0, strlen($data)),1);
  26.     $code.=$string;
  27.         // 3.2 加载自定义字体
  28.         $font_file = '1.ttf';
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
  30.         // 把文字填充到画布
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
  33. }
  34. $_SESSION['code']=$code;//存储在session里
  35. for($i=0;$i<200;$i++){
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
  38. }
  39. for($i=0;$i<2;$i++){
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
  42. }
  43. imagepng($image);
  44. imagedestroy($image);
  45.  
  46. ?>
复制代码

form.php
  1. <?php
  2.     if(isset($_REQUEST['code'])){
  3.          session_start();
  4.         if($_REQUEST['code']==$_SESSION['code']){
  5.             echo "<font color='red'>输入正确</font>";
  6.         }else{
  7.             echo "<font color='red'>输入错误</font>";
  8.         }
  9.     }
  10. ?>
  11. <!DOCTYPE html>
  12. <html>
  13.     <head>
  14.         <meta chartset="UTF-8" />
  15.         <style type="text/css" rel="stylesheet">
  16.             a{
  17.                 text-decoration:none;
  18.                 font-size:30px;
  19.                 color:blue;
  20.             }
  21.             a:hover{
  22.                 text-decoration:underline;
  23.             }
  24.         </style>
  25.     </head>
  26.     <body>
  27.         <form action="form.php" method="get">
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
  30.             请输入验证码: <input name="code" /><br />
  31.             <input type="submit" value="提交" />
  32.         </form>
  33.     </body>
  34.     <script type="text/javascript">
  35.         
  36.         function changeCode(){
  37.             var img=document.getElementById('img');
  38.             //img.src='index.php?r='+Math.random();
  39.             img.setAttribute('src','index.php?r='+Math.random());
  40.         }
  41.     </script>
  42. </html>
复制代码

 
 
 
 
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
实现php间隔一段时间执行一次某段代码
实现php间隔一段时间
相关文章
    无相关信息
栏目更新
栏目热门