开始自然是从最简单的功能起步,我第一个任务选择了做一个登录操作,其实也没想象中那么简单。
1、首先自然是连接和创建数据库
这部分我写在model.php中
1
2
3
4
5
6
7
8
9
10
11
|
$username = 'root' ;
$password = '' ;
$host = 'localhost' ;
$database = 'login' ;
//创建连接
$conn =mysqli_connect( $host , $username , $password , $database );
|
2、写前台页面,为了熟练前端框架,使用layui框架界面,前面有一段js代码,来判断用户名密码输入是否为空。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
<!doctype html>
<html>
<script src= "layui.js" ;></script>
<link rel= "stylesheet" href= "layui.css" rel= "external nofollow" ;>
<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语句查询用户名配上密码的结果集,结果集为空,则不存在该用户。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<?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如何实现登录页面的详细内容,感谢大家对服务器之家的支持。