php+js+mysql设计的仿webQQ-<4>登陆和注册

来源:互联网 发布:网络畅销小说排行榜 编辑:程序博客网 时间:2024/06/05 11:10

<6>登陆验证

Js代码

function checkLogin(){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="checkLogin.php?username="+document.getElementById("username").value+"&pwd="+document.getElementById("pwd").value;//把用户名和密码传到checkLogin.php进行验证  xmlhttp.open("GET",url,true);xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    { document.getElementById("error5").innerHTML="<font color=red size=2px>"+xmlhttp.responseText+"</font>" ;if(xmlhttp.responseText=="登录成功")   {   window.location.href="myWebChat.php";   //验证成功后跳转到自己的主页      }    }  } xmlhttp.send(null);}

PHP代码

<?phpheader('Content-Type:text/html;charset=GB2312');include("conn.php");$username=$_GET["username"];$pwd=$_GET["pwd"];if($username==null||$pwd==null)   {     echo "用户名或密码不能为空!";   }else   {    $sql="select * from user where email='$username'";     $result = mysql_query($sql);     $num=mysql_num_rows($result);  if($num>0)    {   $row=mysql_fetch_array($result);if($pwd==$row["pwd"])  {     @session_start(); $_SESSION["email"]=$username; $statesql="update user set state='在线' where email='".$username."'";  //当用户成功登陆后把状态写入数据库 mysql_query($statesql); echo "登录成功";  }else  {     echo "用户名或密码错误!";  }}  else    {   echo "用户名或密码错误!";}   }?>

<7>注册验证

Js代码

function checkRegistration(){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="checkRegistration.php?email="+document.getElementById("email").value+"&nickname="+document.getElementById("nickname").value+           "&pwd="+document.getElementById("password2").value+"&sex="+document.getElementById("sex").value+   "&month="+document.getElementById("month").value+"&day="+document.getElementById("day").value+   "&error1="+document.getElementById("error1").innerHTML+"&error2="+document.getElementById("error2").innerHTML+   "&error3="+document.getElementById("error3").innerHTML+"&error4="+document.getElementById("error4").innerHTML;  xmlhttp.open("GET",url,true);xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {     document.getElementById("error6").innerHTML="<font color=green size=2px>"+xmlhttp.responseText+"</font>";   if(xmlhttp.responseText=="注册成功")   {   window.location.href="myWebChat.php";      }    }  } xmlhttp.send(null);}

PHP代码

<?phpheader('Content-Type:text/html;charset=GB2312');$error1=$_GET["error1"];$error2=$_GET["error2"];$error3=$_GET["error3"];$error4=$_GET["error4"];$error1=strlen($error1);$error2=strlen($error2);$error3=strlen($error3);$error4=strlen($error4);if(($error1==49&&$error2==49&&$error3==49&&$error4==53)||($error1==43&&$error2==43&&$error3==43&&$error4==47)) //仅适合IE和Chrome  {     $email=$_GET["email"];$nickname=$_GET["nickname"];$pwd=$_GET["pwd"];$sex=$_GET["sex"];$month=$_GET["month"];$day=$_GET["day"];include("conn.php");$sql="insert into user values('$email','$pwd','$nickname','$sex','$month','$day','在线')";$result=mysql_query("$sql");if($result)  {      @session_start();  $_SESSION["email"]=$email;    echo "注册成功";  }          }else  {        echo "请完善注册信息!";  }?>

注册验证自认为写的很差,欢迎拍砖!(未完待续)


原创粉丝点击