-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolist.php
executable file
·183 lines (176 loc) · 6.38 KB
/
todolist.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
function connect_mysql() {
$dbc=@mysqli_connect('127.0.0.1', 'root','123456','todolist')
or die('could not connect to mysql');
mysqli_set_charset($dbc,'utf8');
return $dbc;
}
function register($username,$password,$email){
$dbc=connect_mysql();
if($dbc){
$q='insert into user(name,password,email)values(?,?,?)';
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'sss',$name,$password,$email);
$name=$username;
$password=$password;
$email=$email;
mysqli_stmt_execute($stmt);
if(mysqli_stmt_affected_rows($stmt)==1){
$message['register']='success';
$json_message=json_encode($message);
return $json_message;
}else{
$message['register']='system';
$json_message=json_encode($message);
return $json_message;
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}else{
die('Could not connect: ' . mysql_error());
}
}
function login($username,$password){
session_start();
$dbc=connect_mysql();
$user_id;
if($dbc){
$q='select user_id from user where name=? and password=?';
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'ss',$name,$password);
$name=$username;
$password=$password;
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt,$user_id);
if(mysqli_stmt_affected_rows($stmt)==1){
while(mysqli_stmt_fetch($stmt)){
$user_id=$user_id;
}
$_SESSION['username']=$username;
$_SESSION['user_id']=$user_id;
$_SESSION['password']=$password;
$message['login']='login success';
$json_message=json_encode($message);
return $json_message;
}else{
$message['login']='password error';
$json_message=json_encode($message);
return $json_message;
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}else{
die('Could not connect: ' . mysql_error());
}
}
function check_name($username){
$dbc=connect_mysql();
if ($dbc){
$q='select * from user where name=?';
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'s',$name);
$name=$username;
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_affected_rows($stmt)==1){
$message['check_name']='name exit';
$json_message=json_encode($message);
return $json_message;
}else{
$message['check_name']='name no exit';
$json_message=json_encode($message);
return $json_message;
}
mysqli_stmt_close($stmt);
mysql_close($dbc);
}else{
die('Could not connect: ' . mysql_error());
}
}
function logout(){
session_destroy();
setcookie('phpsessid','',time()-3600);
}
function check_password($old_password){
session_start();
$password=$_SESSION['password'];
if($password!=$old_password){
$message['check_password']='password wrong';
$json_message=json_encode($message);
return $json_message;
}else{
$message['check_name']='password correct';
$json_message=json_encode($message);
return $json_message;
}
}
function changepassword($new_password){
session_start();
$dbc=connect_mysql();
$session_user_id=$_SESSION['user_id'];
if ($dbc){
$q='update user set password=? where user_id=?';
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'si',$password,$user_id);
$password=$new_password;
$user_id=$session_user_id;
mysqli_stmt_execute($stmt);
if(mysqli_stmt_affected_rows($stmt)==1){
$message['changepassword']='change success';
$json_message=json_encode($message);
return $json_message;
}else{
$message['changepassword']='system';
$json_message=json_encode($message);
return $json_message;
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}else{
die('Could not connect: ' . mysql_error());
}
}
function show_list($user_id_from_user){
$dbc=connect_mysql();
$q='select content,list_id from list where user_id=?';
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'i',$user_id);
$user_id=$user_id_from_user;
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt,$content,$list_id);
while(mysqli_stmt_fetch($stmt)){
$list['content']=$content;
$list['list_id']=$list_id;
$list_merge[]=$list;
}
$list_json=json_encode($list_merge);
return $list_json;
mysqli_stmt_close($stmt);
mysql_close($dbc);
}
function add_list($form_content){
session_start();
$user_id_from_session=$_SESSION['user_id'];
$dbc=connect_mysql();
$q='insert into list (content ,user_id) values (?,?)';
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'si',$content,$user_id);
$content=$form_content;
$user_id=$user_id_from_session;
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
function delete_list($form_list_id){
$dbc=connect_mysql();
$q='delete from list where list_id=?';
$stmt=mysqli_prepare($dbc,$q);
mysqli_stmt_bind_param($stmt,'i',$list_id);
$list_id=$form_list_id;
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
?>