5月份的时候,郁师傅让我不用框架写个简单的登陆注册上传头像留言板的功能,要熟悉开发。还是贴出来记录一下吧。
- login.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8">
</head>
<body>
<center>
<form action="login.php"method="post">
<label>username<input type="text" name="username"></label>
<br>
<label>password<input type="text" name="password"></label>
<br>
<button type="submit" name="login">login</button>
<button type="submit" name="register">register</button>
</form>
</center>
<body>
</html>
login.php
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
session_start();
function protect($string){
$string = trim(strip_tags(addslashes($string)));
return $string;}
header("Content-type: text/html; charset=utf-8");
$link=mysqli_connect('localhost','root','','webtest');
if(!$link)
{
die('数据库连接失败');
}
else
{
if(isset($_POST['login']))
{
$username = protect($_POST['username']);
$password = protect($_POST['password']);
$query="select * from `user` where `username`= '$username' and `password`= '".md5($password)."'";
$result=mysqli_query($link,$query);
if(!$result)
{
echo "error:".mysqli_error($link);
exit();
}
if (mysqli_num_rows($result)!=0)
{
echo "登陆成功!";
$_SESSION['username']=$username;
header("Location:home.php");
}
else echo "用户名或密码错误!";
}
if(isset($_POST['register']))
{
$username = protect($_POST['username']);
$password = protect($_POST['password']);
/**先检查username是否存在 */
$query="select * from `user` where `username`= '$username' ";
$result=mysqli_query($link,$query);
if(!$result)
{
echo "error:".mysqli_error($link);
exit();
}
if (mysqli_num_rows($result)!=0)
{
die("username已存在");
header("Location:login.html");
}
/**执行插入 */
$query="INSERT INTO `user` SET `username`='$username',"."`password`='".md5($password)."'";
$result=mysqli_query($link,$query);
if(!$result)
{
echo "error:".mysqli_error($link);
exit();
}
else
{
echo "注册成功!";
header("Location:login.html");
}
}
}home.php
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
include "login.php";
$username=$_SESSION['username'];
$link=mysqli_connect('localhost','root','','webtest');
if(!$link)
{
die('数据库连接失败');
}else
{
$query="select * from `touxiang` where `username` = '$username'";
$result=mysqli_fetch_array(mysqli_query($link,$query));
$touxiang=$result['image'];
//header('Content-type: image/JPEG',true);
echo "<center><table style=' text-align: center' border='2'>
<tr><th>头像</th><th>用户名</th></tr>";
echo "<td><img src='data:image/jpeg;base64,$touxiang' width='40' height='40'></td><td>$username</td></tr>";
echo "</table></center>";
//echo $touxiang;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
<title>主页</title>
</head>
<body>
<center>
<form method="post" enctype="multipart/form-data">
<table border="1">
请上传用户头像<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="上传头像"/>
</table>
</form>
<h6>输入留言内容</h6>
<form action="home.php" method="post">
标题:<input type="text" name="title"><br>
内容:<textarea name="content"></textarea><br>
<input type="submit" name="insert">
</form>
<hr>
</center>
</body>
</html>
$link=mysqli_connect('localhost','root','','webtest');
if(!$link)
{
die('数据库连接失败');
}
else
{
/**修改头像功能 */
if(isset($_FILES['file']))
{
if($_FILES['file']['error']==0)
{
echo "上传成功";
}
$fp = fopen($_FILES["file"]["tmp_name"],"rb");
$image =base64_encode(fread($fp,$_FILES["file"]["size"]));
$query="select * from `touxiang` where `username` = '$username'";
$result=mysqli_query($link,$query);
if (mysqli_num_rows($result)!=0)//判断是否存在
{
$query="UPDATE touxiang SET `username`='$username',`image`='$image'";//更改
mysqli_query($link,$query);
}else
{
$query="insert into touxiang SET `username`='$username',`image`='$image'";
mysqli_query($link,$query);
}
}
/**留言板功能 */
if(isset($_POST['insert']))
{
echo '<center><p1>留言板</center>';
$title = protect($_POST['title']);
$content =protect($_POST['content']);
$query="insert into liuyan SET `username`='$username',`title`='$title',`content`='$content'";
$result1=mysqli_query($link,$query);
}
$result2=mysqli_query($link,"select * from liuyan where `username`='$username'");
echo '
<center>
<table border="1"><tr>
<td>标题</td>
<td>内容</td>
</tr>
</center>
' ;
while($row =mysqli_fetch_array($result2))
{
echo "<tr><td>".$row['title']."</td><td>".$row['content']."</tr>";
}
}
mysql数据库
1
2
3
4
5CREATE DATABASE webtest;
USE webtest;
CREATE TABLE `user` (`username` VARCHAR(20),`password` VARCHAR(20) );
CREATE TABLE `touxaing` (`username` VARCHAR(20),`image` MEDIUMBLOB);
CREATE TABLE `liuyan` (`username` VARCHAR(20),`title` VARCHAR(40),`content` VARCHAR(80));
密码md5加密后储存在数据库中,头像base64加密后保存在数据库中,显示的时候用data协议读出来显示。