zoukankan      html  css  js  c++  java
  • php生成验证码图片

    0:效果图

    1:index.php用来生成验证码图片

     1 <?php
     2 session_start();
     3 header ('Content-Type: image/png');
     4 $image=imagecreatetruecolor(100, 30);
     5 //背景颜色为白色
     6 $color=imagecolorallocate($image, 255, 255, 255);
     7 imagefill($image, 20, 20, $color);
     8 // for($i=0;$i<4;$i++){
     9     // $font=6;
    10     // $x=rand(5,10)+$i*100/4;
    11     // $y=rand(8, 15);
    12     // $string=rand(0, 9);
    13     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    14     // imagestring($image, $font, $x, $y, $string, $color);
    15 // }
    16 $code='';
    17 for($i=0;$i<4;$i++){
    18     $fontSize=8;
    19     $x=rand(5,10)+$i*100/4;
    20     $y=rand(5, 15);
    21     $data='abcdefghijklmnopqrstuvwxyz123456789';
    22     $string=substr($data,rand(0, strlen($data)),1);
    23     $code.=$string;
    24     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    25     imagestring($image, $fontSize, $x, $y, $string, $color);
    26 }
    27 $_SESSION['code']=$code;//存储在session里
    28 for($i=0;$i<200;$i++){
    29     $pointColor=imagecolorallocate($image, rand(100, 255), rand(100, 255), rand(100, 255));
    30     imagesetpixel($image, rand(0, 100), rand(0, 30), $pointColor);
    31 }
    32 for($i=0;$i<2;$i++){
    33     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    34     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    35 }
    36 imagepng($image);
    37 imagedestroy($image);
    38 ?>

    2: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             验证码: &nbsp;<img id="img" src="index.php?" onclick="changeCode()"/>
    29             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a>&nbsp;<br />
    30             请输入验证码:&nbsp;<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>
  • 相关阅读:
    java核心学习笔记(三) java集合框架
    java核心学习笔记(二) 学习环境与学习的方法以及java几个包的作用
    java核心学习笔记(一) javaJDK目录阐述
    版本控制工具git的配置
    linux系统管理员之自动化检测工具 nagios及其插件配置
    运维工程师需要的技能
    在线求中位数(优先队列实现) POJ3784
    并查集 poj1308
    stack HDU1022
    hash数组 POJ1840
  • 原文地址:https://www.cnblogs.com/pjdsy/p/4639072.html
Copyright © 2011-2022 走看看