Home >Backend Development >PHP Tutorial >About the development of PHP and jQuery registration module

About the development of PHP and jQuery registration module

不言
不言Original
2018-06-22 11:16:211660browse

This article mainly records the entire process of developing the PHP jQuery registration module, including filling in the column user name, email, password, repeated password, verification code, etc. It is very detailed and is recommended to everyone

Written one A simple PHP jQuery registration module. The columns that need to be filled in include user name, email, password, repeated password and verification code. The functions and requirements of each column are as follows:

When making this module, we largely drew on the functions and styles of NetEase Registration (http://reg.163.com/reg/reg.jsp?product=urs). However, NetEase’s approach to judging each column is that it does not provide any real-time detection results when entering text, and only displays the detection results when the column loses focus. I think this approach will make The user's vision is relatively unified when typing. What he sees is the prompt about the requirements of the column, and there will be no interruption of other information, but at the same time, he will not get the detection prompt of the character being entered. So when I was working on this function, I made corresponding enhancements to some information that I thought needed real-time prompts, such as the length of the username exceeding the limit and the length and strength of the password, and made a simple judgment on the caps lock of the mailbox. .

Note: The submit button type of the form is button instead of submit, so the carriage return (keydown) of all columns is uniformly set to move the focus to the next column, except for the last column verification code, which is in the verification code Using a carriage return (keydown) in a column will trigger a submission event.

Function Analysis

Username column:

Process

①The page will get focus after loading, and the initial description text will appear when it gets focus;

②Click the user name input box with the mouse, and the initial description text will appear;

③Enter the characters, and you will be prompted immediately to see if the length requirement is met;

④When the focus is lost, first judge whether it is empty, which is When it is empty, the prompt cannot be empty; when it is not empty and the length meets the requirements, it starts to detect whether the user name has been registered;

⑤The user name has been registered, and a prompt is given. If it is not registered, it prompts that it can be registered;

⑥When the focus is obtained again, no matter whether there is input in the input box or whether the input meets the requirements, the initial description text will appear

⑦When you press Enter, the focus will be moved to the mailbox column

Picture:

Details

You can use any characters, and the number of words is limited to: Chinese, no more than 7 Chinese characters, English, numbers or symbols The length does not exceed 14 letters, numbers or symbols, that is, it does not exceed 14 characters

Regarding placeholders (character length), a placeholder for a Chinese character is 2, and a placeholder for an English (number) Is 1, you can use the php statement to calculate the length of the character

 <?php
//php.ini开启了php_mbstring.dll扩展
 $str="博客园小dee";
 
 echo (strlen($str)+mb_strlen($str,&#39;utf-8&#39;))/2;

Output: 11

And strlen($str) output is 15: 4*3 3, Chinese characters are encoded in utf-8 The bottom occupies 3 bytes, and English occupies 1.

mb_strlen($str,'utf-8') outputs 7: the length of a Chinese character is 1,

If you use jquery length to output this string, alert($("#uname").val().length), you will get a length of 7,

should pay attention to this.

At the same time, the user name cannot contain spaces at both ends. During detection and registration, the program will automatically filter the spaces at both ends of the user name.

register.html HTML code snippet of the username column:

<!-- 用户名 -->    
<p class="ipt fipt">
   <input type="text" name="uname" id="uname" value="" placeholder="输入用户名" autocomplete="off" />
   <!--提示文字-->
  <span id="unamechk"></span>
</p>

register.js js code for the public part:

$(function(){
  
  //说明文字
  function notice(showMsg,noticeMsg){      
    showMsg.html(noticeMsg).attr("class","notice");
  }
  //显示错误信息
  function error(showMsg,errorMsg){  
    showMsg.html(errorMsg).attr("class","error");
  }  
  //显示正确信息
  function success(showMsg,successMsg){  
    showMsg.html(successMsg)        
            .css("height","20px")
            .attr("class","success");
  }

  //计算字符长度
  function countLen(value){
  
    var len = 0; 
    for (var i = 0; i < value.length; i++) { 
      if (value[i].match(/[^\x00-\xff]/ig) != null) 
      len += 2; 
      else 
      len += 1; 
    } 
    return len;
  }           

  //......
)};

register.js js code for the user name part:

//检测用户名长度
function unameLen(value){
  
    var showMsg = $("#unamechk");

    /* (strlen($str)+mb_strlen($str))/2 可得出限制字符长度的上限,
    * 例如:$str为7个汉字:"博客园记录生活",利用上面的语句可得出14,
    * 同样,14个英文,利用上面的语句同样能得出字符长度为14
    */
    if(countLen(value) > 14){
            
      var errorMsg = &#39;用户名长度不能超过14个英文或7个汉字&#39;;
      error(showMsg,errorMsg);    
    }else if(countLen(value) == 0){
    
      var noticeMsg = &#39;用户名不能为空&#39;;
      notice(showMsg,noticeMsg);
    }else{

      var successMsg = &#39;长度符合要求&#39;;
      success(showMsg,successMsg);
    }

    return countLen(value);
  }

  //用户名
  unameLen($("#uname").val());
  
  $("#uname").focus(function(){
  
          var noticeMsg = &#39;中英文均可,最长为14个英文或7个汉字&#39;;
          notice($("#unamechk"),noticeMsg);
        })
        .click(function(){
          
          var noticeMsg = &#39;中英文均可,最长为14个英文或7个汉字&#39;;
          notice($("#unamechk"),noticeMsg);
        })
        .keyup(function(){
  
          unameLen(this.value);
        }).keydown(function(){
        
          //把焦点移至邮箱栏目
          if(event.keyCode == 13){
            
            $("#uemail").focus();
          }
        })
        .blur(function(){
        
          if($("#uname").val()!="" && unameLen(this.value)<=14 && unameLen(this.value)>0){
            //检测中
            $("#unamechk").html("检测中...").attr("class","loading");
            //ajax查询用户名是否被注册
            $.post("./../chkname.php",{
            
              //要传递的数据
              uname : $("#uname").val()
            },function(data,textStatus){
              
              if(data == 0){
              
                var successMsg = &#39;恭喜,该用户名可以注册&#39;;
                $("#unamechk").html(successMsg).attr("class","success");

                //设置参数
                nameval = true;
              }else if(data == 1){
              
                var errorMsg = &#39;该用户名已被注册&#39;;
                error($("#unamechk"),errorMsg);
              }else{
              
                var errorMsg = &#39;查询出错,请联系网站管理员&#39;;
                error($("#unamechk"),errorMsg);
              }
            });
          }else if(unameLen(this.value)>14){
          
            var errorMsg = &#39;用户名长度不能超过14个英文或7个汉字&#39;;
            error($("#unamechk"),errorMsg);
          }else{
          
            var errorMsg = &#39;用户名不能为空&#39;;
            error($("#unamechk"),errorMsg);
          }
});

//加载后即获得焦点
$("#uname").focus();

checkname.php code:

<?php

  header("charset=utf-8");

  require_once("conn/conn.php");

  if(isset($_POST[&#39;uname&#39;]) && $_POST[&#39;uname&#39;]!=""){
  
    $uname = trim(addslashes($_POST[&#39;uname&#39;]));
  }

  $sql = "select uname from user where uname=&#39;".$uname."&#39;";
  
  if($conne->getRowsNum($sql) == 1){
  
    $state = 1;
  }else if($conne->getRowsNum($sql) == 0){
  
    $state = 0;
  }else{

    echo $conne->msg_error();
  }

  echo $state;

Prompt text (under Chrome)

①When initially gaining focus, gaining focus again, or clicking

②Detect the length in real time when inputting

 

③When deleting to empty space without losing focus, use a blue icon to prompt that it cannot be empty - the user is It does not look abrupt when inputting

④Losses focus and is not empty, detects whether it is registered (very short, fleeting)

⑤When losing focus Empty, can be registered, has been registered

##User name analysis is completed.

Email Column:

Process

①When the column is focused or clicked, a description will appear regardless of whether the column is empty, filled in correctly or filled in incorrectly. Word;

②用户输入时出现下拉菜单显示多种邮件后缀供用户选择;

③失去焦点时首先判断邮箱格式是否正确,如果正确则检测邮箱是否被注册 ;

④在使用回车选择下拉菜单时,将自动填充邮箱栏目;没有出现下拉菜单时,将焦点移至密码栏目

如图:

register.html邮箱栏目HTML代码片段:

<!-- email -->      
 <p class="ipt">
   <input type="text" name="uemail" id="uemail" value="" placeholder="常用邮箱地址" />
   <span id="uemailchk"></span>
   <ul class="autoul"></ul>
 </p>

下拉功能emailup.js同之前的博文《jQuery实现下拉提示且自动填充的邮箱》,略有修改,注意用回车( keydown和keyup事件)在不同情况下触发的不同动作:

$(function(){
  
  //初始化邮箱列表
  var mail = new Array("sina.com","126.com","163.com","gmail.com","qq.com","hotmail.com","sohu.com","139.com","189.cn","sina.cn");

  //把邮箱列表加入下拉
  for(var i=0;i<mail.length;i++){
  
    var $liElement = $("<li class=\"autoli\"><span class=\"ex\"></span><span class=\"at\">@</span><span class=\"step\">"+mail[i]+"</span></li>");

    $liElement.appendTo("ul.autoul");
  }

  //下拉菜单初始隐藏
  $(".autoul").hide();
  
  //在邮箱输入框输入字符
  $("#uemail").keyup(function(){
   
    if(event.keyCode!=38 && event.keyCode!=40 && event.keyCode!=13){

      //菜单展现,需要排除空格开头和"@"开头
      if( $.trim($(this).val())!="" && $.trim(this.value).match(/^@/)==null ) {

        $(".autoul").show();
        //修改
        $(".autoul li").show();

        //同时去掉原先的高亮,把第一条提示高亮
        if($(".autoul li.lihover").hasClass("lihover")) {
          $(".autoul li.lihover").removeClass("lihover");
        }
        $(".autoul li:visible:eq(0)").addClass("lihover");
      }else{//如果为空或者"@"开头
        $(".autoul").hide();
        $(".autoul li:eq(0)").removeClass("lihover");
      }

      //把输入的字符填充进提示,有两种情况:1.出现"@"之前,把"@"之前的字符进行填充;2.出现第一次"@"时以及"@"之后还有字符时,不填充
      //出现@之前
      if($.trim(this.value).match(/[^@]@/)==null){//输入了不含"@"的字符或者"@"开头
        if($.trim(this.value).match(/^@/)==null){

          //不以"@"开头
          //这里要根据实际html情况进行修改
          $(this).siblings("ul").children("li").children(".ex").text($(this).val());
        }
      }else{

        //输入字符后,第一次出现了不在首位的"@"
        //当首次出现@之后,有2种情况:1.继续输入;2.没有继续输入
        //当继续输入时
        var str = this.value;//输入的所有字符
        var strs = new Array();
        strs = str.split("@");//输入的所有字符以"@"分隔
        $(".ex").text(strs[0]);//"@"之前输入的内容
        var len = strs[0].length;//"@"之前输入内容的长度
        if(this.value.length>len+1){

          //截取出@之后的字符串,@之前字符串的长度加@的长度,从第(len+1)位开始截取
          var strright = str.substr(len+1);

          //正则屏蔽匹配反斜杠"\"
          if(strright.match(/[\\]/)!=null){
            strright.replace(/[\\]/,"");
            return false;
          }
         
          //遍历li
          $("ul.autoul li").each(function(){

            //遍历span
            //$(this) li
            $(this).children("span.step").each(function(){

              //@之后的字符串与邮件后缀进行比较
              //当输入的字符和下拉中邮件后缀匹配并且出现在第一位出现
              //$(this) span.step
              if($("ul.autoul li").children("span.step").text().match(strright)!=null && $(this).text().indexOf(strright)==0){
                
                //class showli是输入框@后的字符和邮件列表对比匹配后给匹配的邮件li加上的属性
                $(this).parent().addClass("showli");
                //如果输入的字符和提示菜单完全匹配,则去掉高亮和showli,同时提示隐藏
                
                if(strright.length>=$(this).text().length){
                    
                  $(this).parent().removeClass("showli").removeClass("lihover").hide();
                }
              }else{
                $(this).parent().removeClass("showli");
              }
              if($(this).parent().hasClass("showli")){
                $(this).parent().show();
                $(this).parent("li").parent("ul").children("li.showli:eq(0)").addClass("lihover");
              }else{
                $(this).parent().hide();
                $(this).parent().removeClass("lihover");
              }
            });
          });

          //修改
          if(!$(".autoul").children("li").hasClass("showli")){

            $(".autoul").hide();
          }
        }else{
          //"@"后没有继续输入时
          $(".autoul").children().show();
          $("ul.autoul li").removeClass("showli");
          $("ul.autoul li.lihover").removeClass("lihover");
          $("ul.autoul li:eq(0)").addClass("lihover");
        }
      }
    }//有效输入按键事件结束

    if(event.keyCode == 8 || event.keyCode == 46){
   
     $(this).next().children().removeClass("lihover");
     $(this).next().children("li:visible:eq(0)").addClass("lihover");
    }//删除事件结束 
    
    if(event.keyCode == 38){
     //使光标始终在输入框文字右边
     $(this).val($(this).val());
    }//方向键↑结束
    
    if(event.keyCode == 13){
    
      //keyup时只做菜单收起相关的动作和去掉lihover类的动作,不涉及焦点转移
      $(".autoul").hide();
      $(".autoul").children().hide();
      $(".autoul").children().removeClass("lihover");     
    }
  });  
  
  $("#uemail").keydown(function(){

    if(event.keyCode == 40){

      //当键盘按下↓时,如果已经有li处于被选中的状态,则去掉状态,并把样式赋给下一条(可见的)li
      if ($("ul.autoul li").is(".lihover")) {

        //如果还存在下一条(可见的)li的话
        if ($("ul.autoul li.lihover").nextAll().is("li:visible")) {

          if ($("ul.autoul li.lihover").nextAll().hasClass("showli")) {

            $("ul.autoul li.lihover").removeClass("lihover")
                .nextAll(".showli:eq(0)").addClass("lihover");
          } else {

            $("ul.autoul li.lihover").removeClass("lihover").removeClass("showli")
                .next("li:visible").addClass("lihover");
            $("ul.autoul").children().show();
          }
        } else {

          $("ul.autoul li.lihover").removeClass("lihover");
          $("ul.autoul li:visible:eq(0)").addClass("lihover");
        }
      } 
    }

    if(event.keyCode == 38){

      //当键盘按下↓时,如果已经有li处于被选中的状态,则去掉状态,并把样式赋给下一条(可见的)li
      if($("ul.autoul li").is(".lihover")){

        //如果还存在上一条(可见的)li的话
        if($("ul.autoul li.lihover").prevAll().is("li:visible")){


          if($("ul.autoul li.lihover").prevAll().hasClass("showli")){

            $("ul.autoul li.lihover").removeClass("lihover")
                .prevAll(".showli:eq(0)").addClass("lihover");
          }else{

            $("ul.autoul li.lihover").removeClass("lihover").removeClass("showli")
                .prev("li:visible").addClass("lihover");
            $("ul.autoul").children().show();
          }
        }else{

          $("ul.autoul li.lihover").removeClass("lihover");
          $("ul.autoul li:visible:eq("+($("ul.autoul li:visible").length-1)+")").addClass("lihover");
        }
      }else{

        //当键盘按下↓时,如果之前没有一条li被选中的话,则第一条(可见的)li被选中
        $("ul.autoul li:visible:eq("+($("ul.autoul li:visible").length-1)+")").addClass("lihover");
      }
    } 

    if(event.keyCode == 13){              

      //keydown时完成的两个动作 ①填充 ②判断下拉菜单是否存在,如果不存在则焦点移至密码栏目。注意下拉菜单的收起动作放在keyup事件中。即当从下拉菜单中选择邮箱的时候按回车不会触发焦点转移,而选择完毕菜单收起之后再按回车,才会触发焦点转移事件
      if($("ul.autoul li").is(".lihover")) {

        $("#uemail").val($("ul.autoul li.lihover").children(".ex").text() + "@" + $("ul.autoul li.lihover").children(".step").text());
      }

      //把焦点移至密码栏目
      if($(".autoul").attr("style") == "display: none;"){
  
        $("#upwd").focus();
      }
    }
  });

  
  //把click事件修改为mousedown,避免click事件时短暂的失去焦点而触发blur事件
  $(".autoli").mousedown(function(){
 
    $("#uemail").val($(this).children(".ex").text()+$(this).children(".at").text()+$(this).children(".step").text());
    $(".autoul").hide();
    
    //修改
    $("#uemail").focus();
  }).hover(function(){

    if($("ul.autoul li").hasClass("lihover")){

      $("ul.autoul li").removeClass("lihover");
    }
    $(this).addClass("lihover");
  });

  $("body").click(function(){

    $(".autoul").hide();
  });
});

register.js邮箱代码片段:

//邮箱下拉js单独引用emailup.js
$("#uemail").focus(function(){
  
          var noticeMsg = &#39;用来登陆网站,接收到激活邮件才能完成注册&#39;;
          notice($("#uemailchk"),noticeMsg);
        })
        .click(function(){
  
          var noticeMsg = &#39;用来登陆网站,接收到激活邮件才能完成注册&#39;;
          notice($("#uemailchk"),noticeMsg);
        })
        .blur(function(){
        
          if(this.value!="" && this.value.match(/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/)!=null){
          
            //检测是否被注册
            $("#uemailchk").html("检测中...").attr("class","loading");
            //ajax查询用户名是否被注册
            $.post("./../chkemail.php",{
            
              //要传递的数据
              uemail : $("#uemail").val()
            },function(data,textStatus){
              
              if(data == 0){
              
                var successMsg = &#39;恭喜,该邮箱可以注册&#39;;
                $("#uemailchk").html(successMsg).attr("class","success");

                emailval = true;
              }else if(data == 1){
              
                var errorMsg = &#39;该邮箱已被注册&#39;;
                error($("#uemailchk"),errorMsg);
              }else{
              
                var errorMsg = &#39;查询出错,请联系网站管理员&#39;;
                error($("#uemailchk"),errorMsg);
              }
            });
          }else if(this.value == ""){
          
            var errorMsg = &#39;邮箱不能为空&#39;;
            error($("#uemailchk"),errorMsg);
          }else{
          
            var errorMsg = &#39;请填写正确的邮箱地址&#39;;
            $("#uemailchk").html(errorMsg).attr("class","error");
          }
});

提示文字( Chrome下 )

①获得焦点时、点击时

②输入时

③失去焦点为空、格式错误、已被注册、可以注册时分别为

邮箱功能至此结束。

密码栏目:

要求

①6-16个个字符,区分大小写(参考豆瓣和网易)

②密码不能为同一字符

③实时提示是否符合要求以及判断并显示密码强度,:

  1.输入时如果为空(删除时)则用蓝色符号提示不能为空,超过长度时用红色符号

  2.密码满足长度但是为相同字符的组合时:密码太简单,请尝试数字、字母和下划线的组合

  3.密码强度判断有多种规则,有直接依据长度和组合规则作出判断,也有给每种长度和组合设置分数,通过验证实际密码的情况计算出最后分数来判断强弱。在这个模块中采用比较简单的一种形式,也是网易注册采用的方法:

  密码满足长度且全部为不同字母、全部为不同数字或全部为不同符号时为弱:弱:试试字母、数字、符号混搭

   密码满足长度且为数字、字母和符号任意两种组合时为中

   密码满足长度且为数字、字母和符号三种组合时为强

④输入时大写提示

如图:

register.html密码栏目HTML代码片段:

<p class="ipt">
  <input type="password" name="upwd" id="upwd" value="" placeholder="设置密码" />
  <p class="upwdpic">
    <span id="upwdchk"></span>
    <img id="pictie" />
  </p>
</p>

register.js密码代码片段:

function noticeEasy(){
  
    //密码全部为相同字符或者为123456,用于keyup时的notice
    var noticeMsg = &#39;密码太简单,请尝试数字、字母和下划线的组合&#39;;
    return notice($("#upwdchk"),noticeMsg);
  }

  function errorEasy(){
  
    //密码全部为相同字符或者为123456,用于blur时的error
    var errorMsg = &#39;密码太简单,请尝试数字、字母和下划线的组合&#39;;
    return error($("#upwdchk"),errorMsg);
  }
  
  //检测密码长度函数
  //检测密码长度
  function upwdLen(value,func){
  
    var showMsg = $("#upwdchk");

    if(countLen(value) > 16){
            
      var errorMsg = &#39;密码不能超过16个字符&#39;;
      error(showMsg,errorMsg);
      
      $("#pictie").hide();
    }else if(countLen(value) < 6){
    
      //使用notice更加友好
      var noticeMsg = &#39;密码不能少于6个字符&#39;;
      notice(showMsg,noticeMsg);

      $("#pictie").hide();
    }else if(countLen(value) == 0){
    
      //使用notice更加友好
      var noticeMsg = &#39;密码不能为空&#39;;
      notice(showMsg,noticeMsg);

      $("#pictie").hide();
    }else{
    
      upwdStrong(value,func);//如果长度不成问题,则调用检测密码强弱
    }

    return countLen(value);//返回字符长度
  }

  //检测密码强弱
  function upwdStrong(value,func){
  
    var showMsg = $("#upwdchk");

    if(value.match(/^(.)\1*$/)!=null || value.match(/^123456$/)){
    
      //密码全部为相同字符或者为123456,调用函数noticeEasy或errorEasy
      func;
    }else if(value.match(/^[A-Za-z]+$/)!=null || value.match(/^\d+$/)!=null || value.match(/^[^A-Za-z0-9]+$/)!=null){

      //全部为相同类型的字符为弱
      var successMsg = &#39;弱:试试字母、数字、符号混搭&#39;;
      success(showMsg,successMsg);

      //插入强弱条
      $("#pictie").show().attr("src","images/weak.jpg");

      pwdval = true;

    }else if(value.match(/^[^A-Za-z]+$/)!=null || value.match(/^[^0-9]+$/)!=null || value.match(/^[a-zA-Z0-9]+$/)!=null){
    
      //任意两种不同类型字符组合为中强( 数字+符号,字母+符号,数字+字母 )
      var successMsg = &#39;中强:试试字母、数字、符号混搭&#39;;
      success(showMsg,successMsg);

      $("#pictie").show().attr("src","images/normal.jpg");

      pwdval = true;
    }else{
    
      //数字、字母和符号混合
      var successMsg = &#39;强:请牢记您的密码&#39;;
      success(showMsg,successMsg);

      $("#pictie").show().attr("src","images/strong.jpg");

      pwdval = true;
    }
  }
  
  $upper = $("<p id=\"upper\">大写锁定已打开</p>");
  
  $("#upwd").focus(function(){
  
          var noticeMsg = &#39;6到16个字符,区分大小写&#39;;
          notice($("#upwdchk"),noticeMsg);

          $("#pictie").hide();
      })
       .click(function(){
      
          var noticeMsg = &#39;6到16个字符,区分大小写&#39;;
          notice($("#upwdchk"),noticeMsg);

          $("#pictie").hide();
      }).keydown(function(){
      
          //把焦点移至邮箱栏目
          if(event.keyCode == 13){
            
            $("#rupwd").focus();
          }
      })
       .keyup(function(){
       
          //判断大写是否开启
          //输入密码的长度
          var len = this.value.length;          
          if(len!=0){

            //当输入的最新以为含有大写字母时说明开启了大写锁定
            if(this.value[len-1].match(/[A-Z]/)!=null){
            
              //给出提示
              $upper.insertAfter($(".upwdpic"));
            }else{
            
              //移除提示
              $upper.remove();
            }
          }else{
          
            //当密码框为空时移除提示
            if($upper){
            
              $upper.remove();
            }
          }//判断大写开启结束
      
          //判断长度及强弱
          upwdLen(this.value,noticeEasy());  
       })
       //keyup事件结束
        .blur(function(){
        
          upwdLen(this.value,errorEasy());
          //upwdLen函数中部分提示使用notice是为了keyup事件中不出现红色提示,而blur事件中则需使用error标红
          if(this.value == ""){
          
            var errorMsg = &#39;密码不能为空&#39;;
            error($("#upwdchk"),errorMsg);

            $("#pictie").hide();
          }else if(countLen(this.value)<6){
          
            var errorMsg = &#39;密码不能少于6个字符&#39;;
            error($("#upwdchk"),errorMsg);

            $("#pictie").hide();
          }
});

大写锁定的思路是:判断输入的字符的最新一位是否是大写字母,如果是大写字母,则提示大写锁定键打开。这种方法并不十分准确,网上有一些插件能判断大写锁定,在这里只是简单地做了一下判断。

提示文字( Chrome下 )

①获得焦点、点击时

②输入时

失去焦点时与此效果相同

失去焦点时与此效果相同

失去焦点时与此效果相同

失去焦点时与此效果相同

③失去焦点为空时

④出现大写时

密码栏目至此结束。

重复密码:失去焦点时判断是否和密码一致

reister.html代码片段:

<p class="ipt">
  <input type="password" name="rupwd" id="rupwd" value="" placeholder="确认密码" />
  <span id="rupwdchk"></span>
</p>

register.js代码片段:

$("#rupwd").focus(function(){
  
          var noticeMsg = &#39;再次输入你设置的密码&#39;;
          notice($("#rupwdchk"),noticeMsg);
      })
        .click(function(){
      
          var noticeMsg = &#39;再次输入你设置的密码&#39;;
          notice($("#rupwdchk"),noticeMsg);
      }).keydown(function(){
      
          //把焦点移至邮箱栏目
          if(event.keyCode == 13){
            
            $("#yzm").focus();
          }
      })
        .blur(function(){
      
          if(this.value == $("#upwd").val() && this.value!=""){
          
            success($("#rupwdchk"),"");

            rpwdval = true;
          }else if(this.value == ""){
          
            $("#rupwdchk").html("");
          }else{
          
            var errorMsg = &#39;两次输入的密码不一致&#39;;
            error($("#rupwdchk"),errorMsg);
          }
});

提示文字:

①获得焦点、点击时

②失去焦点时和密码不一致、一致时分别为

至此重复密码结束。

验证码:不区分大小写

验证码采用4位,可以包含的字符为数字1-9,字母a-f

点击验证码和刷新按钮都能刷新验证码

register.html验证码代码部分:

<p class="ipt iptend">
  <input type=&#39;text&#39; id=&#39;yzm&#39; name=&#39;yzm&#39; placeholder="验证码">
  <img id=&#39;yzmpic&#39; src=&#39;&#39; style="cursor:pointer"> <!-- 验证码图片 -->
  <a style="cursor:pointer" id=&#39;changea&#39;>
    <img id="refpic" src="images/ref.jpg" alt="验证码"> <!-- 验证码刷新按钮图片 -->
  </a>
  <span id=&#39;yzmchk&#39;></span>
  <input type=&#39;hidden&#39; id=&#39;yzmHiddenNum&#39; name=&#39;yzmHiddenNum&#39; value=&#39;&#39;> <!-- 隐藏域,内容是验证码输出的数字,用户输入的字符与其进行对比 -->
</p>

register.js验证码部分:

//验证码按钮
$("#refpic").hover(function(){
    
      $(this).attr("src","images/refhover.jpg");
    },function(){
    
      $(this).attr("src","images/ref.jpg");
    }).mousedown(function(){
    
      $(this).attr("src","images/refclick.jpg");
    }).mouseup(function(){
    
      $(this).attr("src","images/ref.jpg");
    });
    
    //生成验证码函数
    function showval() {

      num = &#39;&#39;;
      for (i = 0; i < 4; i++) {

        tmp = Math.ceil(Math.random() * 15);//Math.ceil上取整;Math.random取0-1之间的随机数
        if (tmp > 9) {
          switch (tmp) {
            case(10):
              num += &#39;a&#39;;
              break;
            case(11):
              num += &#39;b&#39;;
              break;
            case(12):
              num += &#39;c&#39;;
              break;
            case(13):
              num += &#39;d&#39;;
              break;
            case(14):
              num += &#39;e&#39;;
              break;
            case(15):
              num += &#39;f&#39;;
              break;
          }
        } else {
          num += tmp;
        }

        $(&#39;#yzmpic&#39;).attr("src","../valcode.php?num="+num);
      }
      $(&#39;#yzmHiddenNum&#39;).val(num);
    }

    //生成验证码以及刷新验证码
    showval();
    $(&#39;#yzmpic&#39;).click(function(){
    
      showval();
    });
    $(&#39;#changea&#39;).click(function(){
    
      showval();
    });

    //验证码检验
    function yzmchk(){
    
      if($("#yzm").val() == ""){
      
        var errorMsg = &#39;验证码不能为空&#39;;
        error($("#yzmchk"),errorMsg);
      }else if($("#yzm").val().toLowerCase()!=$("#yzmHiddenNum").val()){
      
        //不区分大小写
        var errorMsg = &#39;请输入正确的验证码&#39;;
        error($("#yzmchk"),errorMsg);
      }else{
      
        success($("#yzmchk"),"");

        yzmval = true;
      }
    }

    //验证码的blur事件
    $("#yzm").focus(function(){
    
      var noticeMsg = &#39;不区分大小写&#39;;
      notice($("#yzmchk"),noticeMsg);
    }).click(function(){
    
      var noticeMsg = &#39;不区分大小写&#39;;
      notice($("yzmdchk"),noticeMsg);
    }).keydown(function(){
      
      //提交
      if(event.keyCode == 13){        
        
        //先检验后提交
        yzmchk();
        formsub();
      }
    }).blur(function(){
    
      yzmchk();
});

valcode.php验证码生成php代码:

<?php 

  header("content-type:image/png");
  $num = $_GET[&#39;num&#39;];
  $imagewidth = 150;
  $imageheight = 54;
  
  //创建图像
  $numimage = imagecreate($imagewidth, $imageheight);
  
  //为图像分配颜色
  imagecolorallocate($numimage, 240,240,240); 

  //字体大小
  $font_size = 33;
  
  //字体名称
  $fontname = &#39;arial.ttf&#39;;
  
  //循环生成图片文字
  for($i = 0;$i<strlen($num);$i++){
    
    //获取文字左上角x坐标
    $x = mt_rand(20,20) + $imagewidth*$i/5;
    
    //获取文字左上角y坐标
    $y = mt_rand(40, $imageheight);
    
    //为文字分配颜色
    $color = imagecolorallocate($numimage, mt_rand(0,150), mt_rand(0,150), mt_rand(0,150));
    
    //写入文字
    imagettftext($numimage,$font_size,0,$x,$y,$color,$fontname,$num[$i]);
  }
  
  //生成干扰码
  for($i = 0;$i<2200;$i++){
    $randcolor = imagecolorallocate($numimage, rand(200,255), rand(200,255), rand(200,255));
    imagesetpixel($numimage, rand()%180, rand()%90, $randcolor);
  }
  
  //输出图片
  imagepng($numimage);
  imagedestroy($numimage);
?>

注:把字体"Arial"放在服务器的相应目录

提示文字:

①获得焦点时、点击时

②为空且失去焦点时

③输入错误、输入正确且失去焦点时分别为

验证码至此结束。

使用协议:默认勾选;

register.html相应代码:

<span class="fuwu">
    <input type="checkbox" name="agree" id="agree" checked="checked">
    <label for="agree">我同意 <a href="#">" 服务条款 "</a> 和 <a href="#">" 网络游戏用户隐私权保护和个人信息利用政策 "</a>
    </label>
</span>

register.js相应代码:

if($("#agree").prop("checked") == true){
  
    fuwuval = true;
  }

$("#agree").click(function(){
  
    if($("#agree").prop("checked") == true){

      fuwuval = true;
      $("#sub").css("background","#69b3f2");
    }else{
    
      $("#sub").css({"background":"#f2f2f2","cursor":"default"});
    }  
});

效果图:

①勾选之后

②未勾选

提交按钮:检测是否所有栏目都填写正确,否则所有填写错误的栏目将给出错误提示。全部填写正确后提交并且发送验证邮件到注册邮箱中,邮件的验证地址在3日后失效

首先在register.js开始部分定义几个参数:nameval,emailval,pwdval,rpwdval,yzmval,fuwuval,全部设为0;当相应栏目符合规定之后,把相应的参数设为true。当所有的参数都为true之后,提交至registerChk.php,否则return false。

register.html相应代码:

<button type="button" id="sub">立即注册</button>

register.js相应代码:

参数设置:

var nameval,emailval,pwdval,rpwdval,yzmval,fuwuval = 0;

提交事件:

function formsub(){
  
    if(nameval != true || emailval!=true || pwdval!=true || rpwdval!=true || yzmval!=true || fuwuval!=true){
    
      //当邮箱有下拉菜单时点击提交按钮时不会自动收回菜单,因为下面的return false,所以在return false之前判断下拉菜单是否弹出
      if(nameval != true && $("#unamechk").val()!=""){
      
        var errorMsg = &#39;请输入用户名&#39;;
        error($("#namechk"),errorMsg);
      }

      if($(".autoul").show()){
      
        $(".autoul").hide();
      }

      //以下是不会自动获得焦点的栏目如果为空时,点击注册按钮给出错误提示
      if($("#uemail").val() == ""){
      
        var errorMsg = &#39;邮箱不能为空&#39;;
        error($("#uemailchk"),errorMsg);    
      }

      if($("#upwd").val() == ""){
      
        var errorMsg = &#39;密码不能为空&#39;;
        error($("#upwdchk"),errorMsg);    
      }

      if($("#rupwd").val() == ""){
      
        var errorMsg = &#39;请再次输入你的密码&#39;;
        error($("#rupwdchk"),errorMsg);    
      }

      if($("#yzm").val() == ""){
      
        var errorMsg = &#39;验证码不能为空&#39;;
        error($("#yzmchk"),errorMsg);    
      }

    }else{
    
      $("#register-form").submit();
    }
  }

  $("#sub").click(function(){
    
    formsub();
});

显示效果:

有栏目为空时点击提交按钮

注册以及发送邮件

使用了Zend Framework( 1.11.11 )中的zend_email组件。Zend Framework的下载地址是:https://packages.zendframework.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip。在Zend Framework根目录下library路径下,剪切Zend文件至服务器下,在注册页面中引入Zend/Mail/Transport/Smtp.php和Zend/Mail.php两个文件。

当点击提交按钮时,表单将数据提交至register_chk.php,然后页面在当前页跳转至register_back.html,通知用户注册结果并且进邮箱激活。

验证邮箱的地址参数使用用户名和一个随机生成的key。

register_chk.php:

<?php 

  include_once &#39;conn/conn.php&#39;;
  include_once &#39;Zend/Mail/Transport/Smtp.php&#39;;
  include_once &#39;Zend/Mail.php&#39;;
  
  //激活key,生成的随机数
  $key = md5(rand());
  
  //先写入数据库,再发邮件
  //写入数据库
  //判断是否开启magic_quotes_gpc
  if(get_magic_quotes_gpc()){
  
    $postuname = $_POST[&#39;uname&#39;];
    $postupwd = $_POST[&#39;upwd&#39;];
    $postuemail = $_POST[&#39;uemail&#39;];
  }else{
    
    $postuname = addslashes($_POST[&#39;uname&#39;]);
    $postupwd = addslashes($_POST[&#39;upwd&#39;]);
    $postuemail = addslashes($_POST[&#39;uemail&#39;]);
  }

  function check_input($value){
  
    // 如果不是数字则加引号
    if (!is_numeric($value)){
    
      $value = mysql_real_escape_string($value);
    }
    return $value;
  }
  
  $postuname = check_input($postuname);
  $postupwd = check_input($postupwd);
  $postuemail = check_input($postuemail);

  $sql = "insert into user(uname,upwd,uemail,activekey)values(&#39;".trim($postuname)."&#39;,&#39;".md5(trim($postupwd))."&#39;,&#39;".trim($postuemail)."&#39;,&#39;".$key."&#39;)";

  $num = $conne->uidRst($sql);
  if($num == 1){
    
    //插入成功时发送邮件
    //用户激活链接
    $url = &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].&#39;/php/myLogin/activation.php&#39;;
    //urlencode函数转换url中的中文编码
    //带反斜杠
    $url.= &#39;?name=&#39;.urlencode(trim($postuname)).&#39;&k=&#39;.$key;
    //定义登录使用的邮箱
    $envelope = &#39;dee1566@126.com&#39;;
    
    //激活邮件的主题和正文
    $subject = &#39;激活您的帐号&#39;;
    $mailbody = &#39;注册成功,<a href="&#39;.$url.&#39;" target="_blank">请点击此处激活帐号</a>&#39;;
    
    //发送邮件
    //SMTP验证参数
    $config = array(
        
        &#39;auth&#39;=>&#39;login&#39;,
        &#39;port&#39; => 25,
        &#39;username&#39;=>&#39;dee1566@126.com&#39;,
        &#39;password&#39;=>&#39;你的密码&#39;
        );
    
    //实例化验证的对象,使用gmail smtp服务器
    $transport = new Zend_Mail_Transport_Smtp(&#39;smtp.126.com&#39;,$config);
    $mail = new Zend_Mail(&#39;utf-8&#39;);
    
    $mail->addTo($_POST[&#39;uemail&#39;],&#39;获取用户注册激活链接&#39;);
    $mail->setFrom($envelope,&#39;发件人&#39;);
    $mail->setSubject($subject);
    $mail->setBodyHtml($mailbody);
    $mail->send($transport);

    echo "<script>self.location=\"templets/register_back.html\";</script>";

  }else{
  
    echo "<script>self.location=\"templets/register_back.html?error=1\";</script>";
  }
?>

邮箱中收取的邮件截图:

然后点击邮箱中的链接进行激活,把数据库中的active设置为1。

activation.php:

<?php 
session_start();
header(&#39;Content-type:text/html;charset=utf-8&#39;);
include_once &#39;conn/conn.php&#39;;

$table = "user";

if(!empty($_GET[&#39;name&#39;]) && !is_null($_GET[&#39;name&#39;])){
          
  //urlencode会对字符串进行转义。所以这里要进行处理
  if(get_magic_quotes_gpc()){
  
    $getname = stripslashes(urldecode($_GET[&#39;name&#39;]));
  }else{
  
    $getname = urldecode($_GET[&#39;name&#39;]);
  }

  //urldecode反转url中的中文编码
  $sql = "select * from ".$table." where uname=&#39;".$getname."&#39; and activekey=&#39;".$_GET[&#39;k&#39;]."&#39;";

  $num = $conne->getRowsNum($sql);
  if($num>0){
    
    $rs = $conne->getRowsRst($sql);  

    //此时数据库里的字符串是不会带反斜杠的  
    //因此要为下面的SQL语句加上反斜杠
    $rsname = addslashes($rs[&#39;uname&#39;]);

    $upnum = $conne->uidRst("update ".$table." set active = 1 where uname = &#39;".$rsname."&#39; and activekey = &#39;".$rs[&#39;activekey&#39;]."&#39;");

    if($upnum>0){
      
      $_SESSION[&#39;name&#39;] = urldecode($getname);
      echo "<script>alert(&#39;您已成功激活&#39;);window.location.href=&#39;main.php&#39;;</script>";
    }else{
      
      echo "<script>alert(&#39;您已经激活过了&#39;);window.location.href=&#39;main.php&#39;;</script>";
    }
  }else{
    
    echo "<script>alert(&#39;激活失败&#39;);window.location.href=&#39;templets/register.html&#39;;</script>";
  }
}

?>

关于注册成功后的邮件页和跳转页,这里不做了。

关于数据库防注入的几种方式magic_quote_gpc,addslashes/stripslashes,mysql_real_eascpae_string,我做了一张表格

数据库设计:

user表

create table user (id int primary key auto_increment,
uname varchar(14) not null default &#39;&#39;,
upwd char(32) not null default &#39;&#39;,
uemail varchar(50) not null default &#39;&#39;,
active tinyint(4) default &#39;0&#39;,
activekey char(32) not null defalut &#39;&#39;)engine=innodb default charset=utf8

说明:md5的长度是32。

模块的目录结构如下:

ROOT:
├─conn
│ ├─conn.php

├─templets
│ ├─css
│ │ ├─common.css
│ │ ├─register.css
│ │
│ ├─images
│ │
│ └─js
│ ├─jquery-1.8.3.min.js
│ ├─register.js
│ ├─emailup.js

├─chkname.php
├─chkemail.php
├─valcode.php
├─register_chk.php
├─activation.php
├─arial.ttf

└─Zend

模块至此结束。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

利用PHP生成便于打印的网页的方法

利用html静态页面调用php文件的方法

The above is the detailed content of About the development of PHP and jQuery registration module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn