ajax PHP该如何实现注册,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。ajax php实现注册的方法:1、创建login.html注册登录页面;2、通过J
ajax PHP该如何实现注册,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
ajax php实现注册的方法:1、创建login.html注册登录页面;2、通过Jquery代码实现验证码验证;3、使用PHP代码userLogin.php和addUser.php实现用户登录和注册功能即可。
本文操作环境:windows7系统、PHP7.1版、Dell G3电脑。
ajax php怎么实现注册?
jQuery+ajax+php实现注册登录功能
html代码,login.html
<fORM><label>用户名</label><input id="user-name" type="text" name="" /><label>密码</label><input id="user-passWord" type="password" name="" /><label>验证码</label><input id="code" type="text" name="" /><img src="php/getVerify.php" alt="" /><p class="clear"></p><button type="button" id="login">登录</button><button type="button" id="sign">注册</button></form>
jquery代码,login.js
$('img').click(function(){$('img').attr('src','php/getVerify.php?'+Math.random());//刷新验证码})$('#login').click(function(){var username=$('#user-name').val();var password=$("#user-password").val();var code=$("#code").val();if(username!=""&&password!=""&&code.length==4){$.ajax({type:"POST",url:"php/userLogin.php",dataType:"JSON",data:{"user_name":username,"password":password,"code":code},success:function(data){switch(data){case 1://普通用户$.cookie("user",username);$.cookie("limit",0);window.location.href="index.php";break;case 2://管理员用户$.cookie("user",username);$.cookie("limit",1);window.location.href="index.php";break;case 3://密码错误alert("密码错误!");break;case 4://用户不存在alert("该用户不存在!");break;case 0://验证码错误alert("验证码不正确!");break;}}})}else{alert("请检查您的输入!");}})$('#sign').click(function(){var username=$('#user-name').val();var password=$("#user-password").val();var code=$("#code").val();if(username!=""&&password!=""&&code.length==4){$.ajax({type:"POST",url:"php/addUser.php",dataType:"JSON",data:{"user_name":username,"password":password,"code":code},success:function(data){switch(data){case 1://用户已存在alert("该用户已存在!请换一个用户名注册。")break;case 2://注册成功alert("注册成功!");$.cookie("user",username);$.cookie("limit",0);window.location.href="index.php";break;case 0://验证码错误alert("验证码不正确!");break;}}})}else{alert("请检查您的输入!");}})
php代码,userLogin.php
<?phpheader("Content-type: text/html; charset=UTF-8");session_start();$name = $_POST['user_name'];$password=$_POST['password'];$code=$_POST['code'];$con=mysql_connect('localhost','root','');if(!$con){die('error:'.Mysql_error());}mysql_select_db('db_name');$result=mysql_query("select * from users where user_name='$name'");if($_SESSION['verify']==$code){if($row=mysql_fetch_array($result)){if($row['password']==$password){if($row['power']==0){echo 1;//普通用户}else{echo 2;//管理员用户}}else{echo 3;//密码错误}}else{echo 4;//用户不存在}}else{echo 0;//验证码错误}
addUser.php
<?phpheader("Content-type: text/html; charset=UTF-8");session_start();$name = $_POST['user_name'];$password=$_POST['password'];$code=$_POST['code'];$con=mysql_connect('localhost','root','');if(!$con){ die('error:'.mysql_error());}mysql_select_db('db_name');$result=mysql_query("select * from users where user_name='$name'");if($_SESSION['verify']==$code){ if($row=mysql_fetch_array($result)){ echo 1;//用户已存在 }else{//注册成功 mysql_query("insert into `users` (`user_name`,`password`) values ('$name','$password')"); echo 2; }}else{ echo 0;}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网PHP编程频道,感谢您对编程网的支持。
--结束END--
本文标题: ajax php该如何实现注册
本文链接: https://www.lsjlt.com/news/306445.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0