Home  >  Article  >  Backend Development  >  php+js+mysql设计的仿webQQ-<1>邮箱验证_PHP教程

php+js+mysql设计的仿webQQ-<1>邮箱验证_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:50:441078browse

最近用php+js+mysql做了一个仿webQQ的课程设计,收获很多,现在将关键的技术总结一下,供大家学习交流。

邮箱验证

用户在注册的时候,会在文本框里输入邮箱,这个时候通过文本框的onblur和onchange事件用Ajax无刷新技术来判断用户输入的邮箱是否合法以及是否与已注册的邮箱冲突。

Js代码

[html]
function checkEmail(Email) 
{    
var xmlhttp; 
if (window.XMLHttpRequest) 
  {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    if(xmlhttp.overrideMimeType) 
    {//设置MIME类别 
       xmlhttp.overrideMimeType("text/xml"); 
    } 
  } 
else 
  {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
  } 
  var url="checkEmail.php?email="+document.getElementById("email").value;    //转到checkEmail.php进行验证 
  xmlhttp.open("GET",url,true); 
xmlhttp.onreadystatechange=function() 
  { 
  if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    {   
       document.getElementById("error1").innerHTML=xmlhttp.responseText; 
    } 
  }  
xmlhttp.send(null); 

function checkEmail(Email)

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
    if(xmlhttp.overrideMimeType)
    {//设置MIME类别
       xmlhttp.overrideMimeType("text/xml");
    }
  }
else
  {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  var url="checkEmail.php?email="+document.getElementById("email").value;    //转到checkEmail.php进行验证
  xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    { 
    document.getElementById("error1").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.send(null);
}

 

PHP代码

[php]
header('Content-Type:text/html;charset=GB2312');    //编码方式设置  
include("conn.php"); 
$email=$_GET["email"]; 
$len=strlen($email); 
if($email==null) 
  { 
     echo "*邮箱不能为空!"; 
  } 
else 
  { 
     if($len>50) 
       { 
           echo "*邮箱不要超过50个字符!"; 
       } 
     else 
       { 
           if(eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email))    //在php中用正则表达式验证邮箱  
             { 
                $sql="select * from user where email='$email'";     //连接数据库进行查询看邮箱是否被用  
                $result = mysql_query($sql); 
                $num=mysql_num_rows($result); 
                if($num>0) 
                  { 
                     echo "*该邮箱已被用!"; 
                  } 
                else 
                  { 
                     echo "*邮箱可用!"; 
                  } 
     
             } 
           else 
             { 
                echo "*该邮箱不可用!"; 
             }    
       }   
  } 
?> 
header('Content-Type:text/html;charset=GB2312');    //编码方式设置
include("conn.php");
$email=$_GET["email"];
$len=strlen($email);
if($email==null)
  {
    echo "*邮箱不能为空!";
  }
else
  {
     if($len>50)
    {
        echo "*邮箱不要超过50个字符!";
    }
  else
    {
        if(eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email))    //在php中用正则表达式验证邮箱
       {
       $sql="select * from user where email='$email'";     //连接数据库进行查询看邮箱是否被用
                $result = mysql_query($sql);
                $num=mysql_num_rows($result);
             if($num>0)
               {
        echo "*该邮箱已被用!";
      }
       else
      {
         echo "*邮箱可用!";
      }
 
    }
     else
       {
     echo "*该邮箱不可用!";
    } 
    } 
  }
?>
 
通过对邮箱验证的学习,我想其他的验证应该很简单了吧!(未完待续)

 

摘自 wyzhangchengjin123


 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478250.htmlTechArticle最近用php+js+mysql做了一个仿webQQ的课程设计,收获很多,现在将关键的技术总结一下,供大家学习交流。 1邮箱验证 用户在注册的时候,会...
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