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
- PHP实现注册登录,并实现注册时动态检查用户名是否可用
- jQuery_ajax实现检查用户名是否已注册
- 使用sharedPreferences实现密码登录和注册用户名重复检查
- ajax实现在注册时检测用户名是否注册过。
- PHP实现注册登录
- ajax实现注册页面动态验证用户名是否已注册,不必提交即可验证。
- ajax实现注册页面动态验证用户名是否已注册,不必提交即可验证
- ajax+php实现注册页面中自动检测用户名是否已经注册的功能
- php实现登录注册功能
- Ajax 注册 检查用户名可不可用
- jquery实现注册时异步检测用户名是否存在
- PHP实现Ajax注册用户名验证---示例
- ajax检查用户名是否被注册
- 无刷新检查用户名是否可以注册
- PHP+Ajax异步通讯实现用户名邮箱验证是否已注册( 2种方法实现)
- ajax异步验证--实现注册页面的用户名检查
- JAVA-----Ajax实现注册用户名唯一性检查
- HTTPXML无刷新检查用户名是否可用前台实现篇
- 开心的小明
- 透过汇编另眼看世界之类成员函数指针
- 时钟程序
- 请帮忙做一个问卷调查
- meanshift与camshift跟踪研究
- PHP实现注册登录,并实现注册时动态检查用户名是否可用
- Android 创建分隔线的两种方法
- UVA 1434 - YAPTCHA(数论)
- ⅡS启用gzip
- ZOJ1622 Switch
- [网络流24题] 搭配飞行员
- POJ 3613 Cow Relays 恰好n步的最短路径
- uva 11014 - Make a Crystal(数论)
- Netty_一些概念