最先自然是从最简朴的功用起步,我第一个使命挑选了做一个登录操纵,实在也没设想中那末简朴。
1、起首自然是衔接和建立数据库
这部份我写在model.php中
$userName='root'; $passWord=''; $host='localhost'; $dataBase='login'; //建立衔接 $conn=mysqli_connect($host,$userName,$passWord,$dataBase);
相干引荐:《php环境搭建》
2、写前台页面,为了闇练前端框架,运用layui框架界面,前面有一段js代码,来推断用户名暗码输入是不是为空。
<!DOCTYPE html> <html> <script src="layui.js";></script> <link rel="stylesheet" href="layui.css" ;> <head> <meta charset="UTF-8"> <title>注册登录</title> </head> <script language=JavaScript> function InputCheck() { if (Login.username.value == "") { alert("请输入用户名!"); Login.username.focus(); return (false); } if (Login.password.value == "") { alert("请输入暗码!"); Login.password.focus(); return (false); } } </script> <body style="background: #1E9FFF"> <div style="position: absolute; left: 50%; top: 50%;width: 500px; margin-left:-250px; margin-top: -200px"> <div style="background: #FFFFFF; padding: 20px;border-radius: 4px;box-shadow: 5px 5px 20px #444444" > <div> <form action="login.php" method="post" name="Login" οnsubmit="return InputCheck()"> <div style="color: gray"> <h2>注册登录体系</h2> </div> <hr> <div> <label>用户名</label> <div> <input type="text" name="username" id="username" placeholder="用户名" autocomplete="off"> </div> </div> <div> <label>密 码</label> <div> <input type="password" name="password" id="password" placeholder="暗码" autocomplete="off"> </div> </div> <div> <div;> <input type="submit" value="登录"> <input type="button" value="注册"> </div> </div> </form> </div> </div> </div> </body> </html>
3、login.php 用来推断用户名暗码的正确性,关于这一点我看了网上的许多要领,八门五花,在我没碰到停滞之前,我决议先用简朴的情势,就是用sql语句查询用户名配上暗码的结果集,结果集为空,则不存在该用户。
<?php //数据库衔接 require_once 'model.php'; //从登录页接收来的数据 $name=$_POST['username']; $pwd=$_POST['password']; $sql="select id,username,password from user where username='$name' AND password='$pwd';"; $result=mysqli_query($conn,$sql); $row=mysqli_num_rows($result); if(!$row){ echo "<script>alert('暗码毛病,请从新输入');location='login.html'</script>"; } else{ echo "<script>alert('登录胜利');location='123'</script>"; };
4、文件目次
5、结果以下:
以上就是php怎样完成登录页面的细致内容,更多请关注ki4网别的相干文章!