给刚刚接触行业的朋[文]友们一点指引吧,少[章]走弯路了
直接上正文
PHP实现登录注册三个文件 index.php (首页) login.php (登录)register.php (注册)
代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Zhe - 注册登录</title> <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" /> <script src="https://www.layuicdn.com/layui/layui.js" charset="utf-8"></script> </head> <body> <fieldset style="margin-top: 20px;"> <legend>Zhe - 登录注册演示</legend> </fieldset> <div style="padding: 20px; background-color: #F2F2F2;"> <div class="jjjccc8a047cc3f9da9c2 layui-row layui-col-space15"> <div class="jjjcc47cc3f9da9c2fe8e layui-col-md6"> <div class="jjjcc3f9da9c2fe8e8aba layui-card"> <div class="jjjcca9c2fe8e8aba6030 layui-card-header">注册</div> <div class="jjjccfe8e8aba6030af14 layui-card-body"> <form action="register.php" method="post" onsubmit="return checkForm(this)" lay-filter="example"> <div class="jjjcc8aba6030af144d76 layui-form-item"> <label class="jjjcc6030af144d76098b layui-form-label">输入框</label> <div class="jjjccaf144d76098b6bc8 layui-input-block"> <input type="text" name="username" placeholder="请输入账号" class="jjjcc4d76098b6bc839e7 layui-input"> </div> </div> <div class="jjjcc098b6bc839e7aa08 layui-form-item"> <label class="jjjcc666b37acdf3ab512 layui-form-label">密码框</label> <div class="jjjcc37acdf3ab51250f8 layui-input-block"> <input type="password" name="password" placeholder="请输入密码" class="jjjccdf3ab51250f8c8a0 layui-input"> </div> </div> <div class="jjjccb51250f8c8a047cc layui-form-item"> <div class="jjjcc50f8c8a047cc3f9d layui-input-block"> <input type="submit" value="立即注册"/> </div> </div> </form> </div> </div> </div> <div class="jjjccc8a047cc3f9da9c2 layui-col-md6"> <div class="jjjcc47cc3f9da9c2fe8e layui-card"> <div class="jjjcc3f9da9c2fe8e8aba layui-card-header">登录</div> <div class="jjjcca9c2fe8e8aba6030 layui-card-body"> <form action="login.php" method="post" onsubmit="return checkForm(this)" lay-filter="example"> <div class="jjjccfe8e8aba6030af14 layui-form-item"> <label class="jjjcc8aba6030af144d76 layui-form-label">输入框</label> <div class="jjjcc6030af144d76098b layui-input-block"> <input type="text" name="username" placeholder="请输入账号" class="jjjccaf144d76098b6bc8 layui-input"> </div> </div> <div class="jjjcc4d76098b6bc839e7 layui-form-item"> <label class="jjjcc098b6bc839e7aa08 layui-form-label">密码框</label> <div class="jjjcc666b37acdf3ab512 layui-input-block"> <input type="password" name="password" placeholder="请输入密码" class="jjjcc37acdf3ab51250f8 layui-input"> </div> </div> <div class="jjjccdf3ab51250f8c8a0 layui-form-item"> <div class="jjjccb51250f8c8a047cc layui-input-block"> <input type="submit" value="立即登录"/> </div> </div> </form> </div> </div> </div> </div> </div> <script type="text/javascript"> // 验证输入不为空的脚本代码 function checkForm(form) { if(form.username.value == "") { alert("用户名不能为空!"); form.username.focus(); return false; } if(form.password.value == "") { alert("密码不能为空!"); form.password.focus(); return false; } return true; } </script> </body> </html> login.php代码:
<?php session_start(); header("content-type:text/html;charset=utf-8"); //连接数据库 $link = mysqli_connect("localhost","pay_com_cn","pay_com_cn","pay_com_cn"); if (!$link) { die("连接失败: " . mysqli_connect_error()); } //接收$_POST用户名和密码 $username = $_POST['username']; $password = $_POST['password']; //查看表user用户名与密码和传输值是否相等 $sql = "SELECT * FROM user WHERE username = '$username' AND password = '$password'"; //result必需规定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符。 $result = mysqli_query($link,$sql); $num = mysqli_num_rows($result);//函数返回结果集中行的数量 //判断是否登录后显示或跳转 if($num){ echo '登录成功'; }else{ echo'登录失败'; } mysqli_close($link);//关闭数据库 ?> register.php代码:
<?php header("content-type:text/html;charset=utf-8"); //连接数据库 $link = mysqli_connect("localhost","pay_com_cn","pay_com_cn","pay_com_cn"); if (!$link) { die("连接失败: " . mysqli_connect_error()); } //接收$_POST用户名和密码 $username=$_POST['username']; $password=$_POST['password']; //查看表user用户名是否存在或为空 $sql_select = "SELECT * FROM user WHERE username = '$username'"; //result必需规定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符。 $select = mysqli_query($link,$sql_select); $num = mysqli_num_rows($select);//函数返回结果集中行的数量 if($username == "" || $password == "") { echo "请确认信息完整性"; }else if($num){ echo "已存在用户名";//已存在账户名输出错误 }else{ $sql="insert into user(username,password) values('$username','$password')"; $result=mysqli_query($link,$sql); //判断是否注册后显示内容 if(!$result) { echo "注册不成功!"."<br>";//输出错误 echo "<a href='https://www.xkwo.com/article/index.php'>返回</a>";//超链接到首页 } else { echo "注册成功!"."<br/>";//输出成功 echo "<a href='https://www.xkwo.com/article/index.hphp'>立刻登录</a>";//超链接到首页 } } ?>
还没有评论,来说两句吧...