PHP登录界面小结

前言:前几天接到任务,让在几天之内写出一个登录界面,要连数据库并且带cookie的那种,接到任务时一脸懵,但还是在几天内还是赶了出来。
废话不多说了,接下来给大家展示一下我那漏洞百出的代码。

1、登录界面

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
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['name']) && isset($_POST['password']) && $_POST['name']!=NULL && $_POST['password']!=NULL)
{
$link=mysqli_connect('localhost','root','root');

if(mysqli_connect_errno())
{
exit(mysqli_connect_errno());
}

mysqli_select_db($link,'usert');

$query="select * from t2 ";

$result=mysqli_query($link,$query);

$a=0;

while($colum=mysqli_fetch_array($result))
{
if($colum['name']===$_POST['name'] && $colum['password']===$_POST['password'])
{
$a++;
break;
}
}

if($a>0)
{
if(setcookie('name',$colum['name'],time()+500))
{
header('Location:index.php');

}
else
{
echo "<script>alert('cookie配置失败');</script>";
}
}
else
{
echo "<script>alert('账号或密码错误,或者没有注册');</script>";
}
mysqli_close($link);
}
else
{
echo "<script>alert('账号或密码错误,或者没有注册');</script>";
}

}

?>

<!DOCTYPE html>
<html>
<head>
<title>登录界面</title>
<meta charset="utf-8" />
<style>

body{
background-image:url(jpg/4.jpg);
background-repeat:no-repeat;
}
#h{
margin-top:200px;
font-size: 22px;
text-align: center;
line-height: 40px;
}
.s{
text-align: center;
font-size:17px;
}
.n{
margin-left:0%;
}
#longin{

margin-left:46%;
width: 178px;
height: 25px;
background: #357eb8;
color: #bcedff;
}

#y{

margin-left:46%;
}
a{
margin-left:4%;
}
</style>
</head>
<body>
<form method="post" action="login.php">
<p id="h">用户登陆</p>

<p class="s">账号:<input type="text" name="name" class="n" /></p>

<p class="s">密码:<input type="password" name="password" class="n" /></p>

<input type="submit" value="登陆" id="longin" name="submit" />


</br>
<p id="y">没有账号?<a href="register.php">立即注册</a> </p>
</form>
</body>
</html>

2、注册界面

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['name']) && isset($_POST['password']) && isset($_POST['repassword']) )
{
if($_POST['password']===$_POST['repassword'])
{

$link=mysqli_connect('localhost','root','root');

if(mysqli_connect_errno())
{
exit(mysqli_connect_errno());
}

mysqli_select_db($link,'usert');

$query='select * from t2';

$result=mysqli_query($link,$query);

$c=0;
while($colum=mysqli_fetch_array($result))

{
if($colum['name']===$_POST['name'])
{
$c++;
break;
}
}

if($c==0)
{
$un=$_POST['name'];

$ps=$_POST['password'];

$query1="insert into t2(name,password) values('$un','$ps')";

mysqli_query($link,$query1);

header('Location:link.php');

}
else
{
echo "<script>alert('用户已存在');</script>";
}
mysqli_close($link);
}
else
{
echo "<script>alert('输入的两次密码不一致');</script>";
}
}
else
{
echo "<script>alert('请填写账号和密码');</script>";
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>注册界面</title>
<meta charset="utf-8" />
<style>
body{
background-image:url(jpg/1.jpg);
background-repeat:no-repeat;
}
#h{
margin-top:100px;
font-size: 22px;
text-align: center;
line-height: 40px;
}
.s{
text-align: center;
font-size:17px;
}
.n{
margin-left:0%;
}
#longin{

margin-left:46%;
background: #357eb8;
color: #bcedff;
}

#y{
margin-left:46%;
}

#m{
margin-left:39.3%;

font-size:17px;

}
#reset{
margin-left:3.5%;
background: #357eb8;
color: #bcedff;

}
</style>
</head>
<body>
<form method="post" action="register.php">
<p id="h">用户注册</p>

<p class="s">账号:<input type="text" class="n" name="name" /></p>

<p class="s">密码:<input type="password" class="n" name="password" /></p>

<p id="m">再次输入密码:<input type="password" class="n" name="repassword" /></p>

<p class="s">邮箱:<input type="text" class="n" /></p>



<input type="submit" value="立刻注册" id="longin" name="submit" />

<input type="reset"value="重置" id="reset" />
</br>

</form>
</body>
</html>

副本文件1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
echo "<p>注册成功<p>";
echo "<br />";
echo "<a href='login.php'>你可以登录了</a>";

?>
<!DOCTYPE html>
<html>
<head>
<title>注册成功</title>
<meta charset="utf-8" />
<style>

p,a{
font-size:100px;
text-align:center;
margin-top:20%;
}

</style>

副本文件2

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
<?php

$link=mysqli_connect('localhost','root','root');

if(mysqli_connect_errno())
{
exit(mysqli_connect_errno());
}

mysqli_select_db($link,'usert');

$query='select * from t2';

$result=mysqli_query($link,$query);
$b=0;

while($search=mysqli_fetch_array($result))
{
if(@($_COOKIE['name']===$search['name']))
{
$b++;
break;
}
}
mysqli_close($link);

if(isset($_COOKIE['name'])&&$b>0)
{
echo "<p>亲爱的{$_COOKIE['name']},欢迎回来</p>";
echo "<a href='longins.php'>注销</a>";
}

else
{
echo "<a href='login.php'>请登录</a>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>欢迎回来</title>
<meta charset="utf-8" />
<style>
</style>

副本文件3

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
$link=mysqli_connect('localhost','root','root');

if(mysqli_connect_errno())
{
exit(mysqli_connect_errno());
}

mysqli_select_db($link,'usert');

$query='select * from t2';

$result=mysqli_query($link,$query);
$b=0;

while($search=mysqli_fetch_array($result))
{
if(($_COOKIE['name']===$search['name']))
{
$b++;
break;
}
}

if(isset($_COOKIE['name'])&&$b>0)
{
if(setcookie('name',$colum['name'],time()-500))
{
header('Location:index.php');
}
else
{
header('Location:index.php');
}

}
?>

总结:说实话这代码写的真的不咋地,里面的bug和其他的一些细节根本都没有考虑,当时只想着完成能完成基本的功能就行了,完全没有考虑其他的。

文章目录
  1. 1. 1、登录界面
  2. 2. 2、注册界面
  3. 3. 副本文件1
  4. 4. 副本文件2
  5. 5. 副本文件3
|