開始自然是從最簡單的功能起步,我第一個任務選擇了做一個登錄操作,其實也沒想象中那么簡單。
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如何實現登錄頁面的詳細內容,感謝大家對服務器之家的支持。