验证码

  1.index.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta http-equiv="X-UA-Compatible" content="IE=edge">
 6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7     <title>Document</title>
 8     <script src="jquery-3.6.0.js"></script>
 9     <script >
10         $(function(){
11             function getcode() {
12                 $(".yzm").attr("src","yanzhengma.php?num="+Date.now());
13             }
14             getcode();
15             
16             $(".yzm").click(function(){
17                 getcode();
18             });
19 
20             $(".submit").click(function () {
21                 $.post("login.php",{"value":$(".tx1").val()},function (data) {
22                     alert(data);
23                 });
24             })
25         });
26     </script>
27 </head>
28 <body>
29     <input class="tx1" type="text">
30     <img class="yzm" alt="">
31     <button class="submit">提交</button>
32 </body>
33 </html>

  2.jquery-3.6.0.js:https://code.jquery.com/jquery-3.6.0.js

  3.login.php

    <?php
        //echo $_SERVER["QUERY_STRING"];
        session_start();
        if($_POST["value"] == $_SESSION["YZcode"]){
            echo  "注册成功";
        } else {
            echo "验证码错误";
        }
    ?>

  4.yanzhengma.php

<?php

    $YZarray = [1,2,3,4,5,6,7,8,9,0];
    $YZcode = "";
    for($i=0;$i<4;$i++){
        $YZcode.=$YZarray[rand(0,count($YZarray)-1)];
    }
    // session_id();
    // session_start();
    // $_SESSION[$YZcode]= $YZcode;

    //保存验证码的文本到session
    preg_match("/\d{13}/",$_SERVER["QUERY_STRING"],$array);
    session_id($array[0]);
    session_start();
    $_SESSION["YZcode"]= $YZcode;

    // var_dump($_POST);
    header("Content-type:image/png");
    //画板宽高
    $width = 160;
    $height = 60;
    //新建画板
    $img = imagecreatetruecolor($width,$height);
    //配置颜色
    $colorBg = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
    $colorBorder = imagecolorallocate($img,rand(0,60),rand(0,60),rand(0,60));
    $colorFont = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
    //填充背景色
    imagefill($img,0,0,$colorBg);
    //画边框
    imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);
    //生成100个干扰点
    for($i=0;$i<100;$i++){
        $colorPixel = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
        imagesetpixel($img,rand(1,$width-2),rand(1,$height-2),$colorPixel);
    }
    //生成5条干扰线
    for($i=0;$i<5;$i++){
        $colorLine = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
        imageline($img,rand(0,$width/3),rand(0,$height),rand(2*$width/3,$width),rand(0,$height),$colorLine);
    }
    //生成文本
    imagettftext($img,rand(35,45),rand(-10,10),rand(5,25),rand(40,60),$colorFont,"STCAIYUN.TTF",$YZcode);
    //输出图像,另存为图片文件
    imagepng($img);
    //关闭画图程序
    imagedestroy($img);

  5.STCAIYUN.TTF:windows系统自带字体,自行提取,或者自行替换别的。(yanzhengma.php)

 

posted @ 2023-04-22 21:06  chuankuan  阅读(180)  评论(0)    收藏  举报

载入天数...载入时分秒...