您当前的位置 :首页 > 学习资料 > 验证码
投稿

验证码

2021-03-21 21:54:58 来源: 作者: 责任编辑:cncml

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>
复制代码

 
 
 
 

文章来源: 责任编辑:cncml
版权声明:
1、本主题所有言论和图片纯属会员个人意见,与本网站立场无关
2、本站所有主题由该文章作者发表,该文章作者与享有文章相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和的同意
4、文章作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、管理员和版主有权不事先通知发贴者而删除本文
不良信息举报信箱 新闻热线:18733599993 技术服务:18733599993 网上投稿
关于本站 | 广告服务 | 免责申明 | 招聘信息 | 联系我们
在线网 版权所有 Copyright(C)2005-2025