PHP實現註冊登入,並實現註冊時動態檢查使用者名稱是否可用
阿新 • • 發佈:2019-01-03
用PHP實現註冊登入,原理就是註冊時往資料庫中插入使用者資訊比如使用者名稱和密碼等,登陸即驗證輸入的賬號密碼在資料庫中是否存在。
1、註冊介面html程式碼:
2、動態檢查使用者名稱是否可用程式碼(checkname.php)<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>
<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"><?php echo "<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(); ?>
<?php
echo "<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>";
}
}
?>