无为清净楼资源网 Design By www.qnjia.com
PHP简易用户登录系统,供大家参考,具体内容如下
最近刚刚看到PHP连接数据库的实例,于是做了一个简易的用户系统
直接上代码
连接数据库:connect.php
<"localhost"; $username = "formbd"; $password = "formbd"; $dbname = "form"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } "htmlcode"><!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户注册页面</title> </head> <body> <form action="reg.php" method="post"> <p>用户名:<input type="text" name="name"></p> <p>密 码: <input type="text" name="password"></p> <p><input type="submit" name="submit" value="注册"> <a href="login.html" ><input type="button" name="login" value="已有账号,返回登录"></a> </p> </form> </body> </html>注册后端处理:reg.php
<"Content-Type: text/html; charset=utf8"); if(!isset($_POST['submit'])){ exit("错误执行"); }//判断是否有submit操作 $name=$_POST['name'];//post获取表单里的name $user_password=$_POST['password'];//post获取表单里的password include('connect.php');//链接数据库 $q="insert into user(id,username,password) values (null,'$name','$user_password')";//向数据库插入表单传来的值的sql $sql = "select * from user where username = '$name'"; if (($conn->query($sql))==$name) { echo '用户名已存在'; $result = $conn->query($sql); /*echo " <script> setTimeout(function(){window.location.href='reg.html';},1000); </script> ";*/ } else { $conn->query($q); echo "注册成功"; echo " <script> setTimeout(function(){window.location.href='login.html';},1000); </script> "; } $conn->close();//关闭数据库 "htmlcode"><!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆</title> </head> <body> <form name="login" action="login.php" method="post"> <p>用户名<input type=text name="name"></p> <p>密 码<input type=password name="password"></p> <p><input type="submit" name="submit" value="登录"> <a href="reg.html" ><input type="button" name="reg" value="注册"></a> </p> </form> </body> </html>登录后端处理:login.php
<"Content-Type: text/html; charset=utf8"); if(!isset($_POST["submit"])){ exit("错误执行"); }//检测是否有submit操作 include('connect.php');//链接数据库 $name = $_POST['name'];//post获得用户名表单值 $passowrd = $_POST['password'];//post获得用户密码单值 if ($name && $passowrd){//如果用户名和密码都不为空 $sql = "select * from user where username = '$name' and password='$passowrd'";//检测数据库是否有对应的username和password的sql $result = $conn->query($sql);//执行sql $rows=$result->fetch_assoc();//返回一个数值 if($rows){//0 false 1 true header("refresh:0;url=success.php");//如果成功跳转至success.php页面 exit; }else{ echo "用户名或密码错误"; echo " <script> setTimeout(function(){window.location.href='login.html';},1000); </script> ";//如果错误使用js 1秒后跳转到登录页面重试; } }else{//如果用户名或密码有空 echo "表单填写不完整"; echo " <script> setTimeout(function(){window.location.href='login.html';},1000); </script>"; //如果错误使用js 1秒后跳转到登录页面重试; } $conn->close();//关闭数据库 "htmlcode"><""; "htmlcode"><!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆成功</title> </head> <body> 欢迎光临 <?php echo $username;?> <?php ?> </body> </html>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】
- 林忆莲.2019-0(2023精装版)【环球】【WAV+CUE】
- Supper.Moment.2016-The.Moment【寰亚】【FLAC分轨】
- 范莉丝.1979-送郎一朵牵牛花(LP版)【大联机构】【WAV+CUE】
- 刘宇.2024-宇宙诗人【哔哟哔哟文化】【FLAC分轨】
- 《全景声白噪音》沉浸式大自然助眠 带你听自然的声音[mp3]
- 纯音入心系列纯音乐《古筝精选 十指弹一赏心曲一心奏出世人心》1CD[MP3][582MB]