PHP实现注册登录,并实现注册时动态检查用户名是否可用

来源:互联网 发布:matlab 编程实践 编辑:程序博客网 时间:2024/05/21 18:40

用PHP实现注册登录,原理就是注册时往数据库中插入用户信息比如用户名和密码等,登陆即验证输入的账号密码在数据库中是否存在。

1、注册界面html代码:

<html><head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>  <script src="js/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>  <script type="text/javascript">     function getdata() {    var name = $("#Name").val();    $.post("checkname.php", { "Name": name }, function(data, status) {      if (status == "success") {        if (data=='0') {          //alert("可以使用"+data);          document.getElementById("show").innerHTML="用户名可用";          $("#show").css("color", "green");          document.getElementById("sub").disabled=false;        }        else {          //alert(data);          document.getElementById("show").innerHTML="用户名已被注册";          document.getElementById("sub").disabled=true;          $("#show").css("color", "red");        }        // document.getElementById("show").innerHTML=data;      }      else {        alert("ajax处理错误")      }    });  }  </script>  <title>用户注册</title></head><body>  <form name="frm" method="post" action="checkreg.php" >    <table width="34%" border="1" align="center">      <tr>        <td width="30%" height="35" align="right" >用户名:</td>        <td width="70%" height="35">          <input type="text" name="Name" id="Name" <span style="color:#cc0000;">onblur="getdata();</span>"/>(2-8个汉字或者5-16位数字或者字母)          <div id="show" style=" float:left;font-size:12px;"></div></td>      </tr>      <tr>        <td width="30%" height="35" align="right">密码:</td>        <td width="70%" height="35"><input type="password" name="Password" />(5-16位数字或者字母)</td>      </tr>      <tr>        <td height="35" colspan="2" align="center"><input type="submit" name="sub" id="sub" value="注册" />          <a href="http://localhost:8080/ldl/login.html">已有账号,去登陆</a></td>        </tr>      </table>    </form>  </body>  </html>
2、动态检查用户名是否可用代码(checkname.php)
<pre name="code" class="php"><?php//echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";$name=$_POST['Name'];$con=mysql_connect("localhost:3306","root","");mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库 if (!$con){  die('Could not connect: ' . mysql_error());}else{  mysql_select_db("userinfo",$con);  $sel = mysql_query("SELECT * FROM userlogin WHERE Name = '$name'");  if ($result = mysql_fetch_object($sel))  {  echo '1';  }  else {  echo '0';  }   exit();}?>


3、实现注册代码(checkreg.php)
<pre name="code" class="php"><?phpecho "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";$name=$_POST['Name'];$password=$_POST['Password'];$n=strlen($name);$p=strlen($password);if($n==0){echo "<script>alert('请输入用户名');history.back();</script>";}elseif(!($n>=5 && $n<=16)){echo "<script>alert('用户名长度为5-16位');history.back();</script>";  }elseif($p==0){echo "<script>alert('请输入密码');history.back();</script>";}elseif(!($p>=5 && $p<=16)){echo "<script>alert('密码长度为5-16位');history.back();</script>";}else{  $con=mysql_connect("localhost:3306","root","");mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库 if (!$con)  {  die('Could not connect: ' . mysql_error());  }  mysql_select_db("userinfo",$con);  $sl="INSERT INTO userlogin (Name,Password)VALUES('$_POST[Name]','$_POST[Password]')";if (!mysql_query($sl,$con))  {  die('Error: ' . mysql_error());  }  else{echo "<script>alert('恭喜你,注册成功');history.back()</script>";mysql_close();}}?>


4、登陆界面(login.html)

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>用户登陆</title></head><body><form name="frm" method="post" action="login.php" ><table width="31%" border="1" align="center">  <tr>    <td width="30%" height="35" align="right" >用户名:</td>    <td width="70%" height="35"><input type="text" name="Name" /></td>  </tr>  <tr>    <td width="30%" height="35" align="right">密码:</td>    <td width="70%" height="35"><input type="password" name="Password" /></td>  </tr>  <tr>    <td height="35" colspan="2" align="center"><input type="submit" name="sub" value="登陆" />    <a href="http://localhost:8080/ldl/register.html">还没有注册,去注册</a></td>  </tr></table></form></body></html>
5、实现登陆代码(login.php)
<?php session_start(); ?><?phpecho "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";$username=$_POST['Name'];$password=$_POST['Password'];$con=mysql_connect("localhost:3306","root","");mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库 if (!$con){  die('Could not connect: ' . mysql_error());}else{  mysql_select_db("userinfo",$con);  $sel = mysql_query("SELECT * FROM userlogin WHERE Name = '$username'");  if ($result = mysql_fetch_object($sel))  {  if($result->Password!=$_POST['Password']){  echo "<script>alert('密码错误');history.back()</script>";  }  else{  $_SESSION['Name']=$username;   echo "<script>alert('登陆成功');    location.href='userpage.php';  </script>";  }  //echo $name;  //echo $password;  }  else {  //echo $name;  //echo $password;   echo "<script>alert('用户名不存在');history.back()</script>";  } }?>



0 1
原创粉丝点击