01.
<?php
02.
session_start();
03.
04.
05.
$username
=
""
;
06.
$email
=
""
;
07.
$errors
=
array
();
08.
09.
10.
$db
= mysqli_connect(
'localhost'
,
'root'
,
'123456789'
,
'registration'
);
11.
12.
13.
if
(isset(
$_POST
[
'reg_user'
])) {
14.
15.
$username
= mysqli_real_escape_string(
$db
,
$_POST
[
'username'
]);
16.
$email
= mysqli_real_escape_string(
$db
,
$_POST
[
'email'
]);
17.
$password_1
= mysqli_real_escape_string(
$db
,
$_POST
[
'password_1'
]);
18.
$password_2
= mysqli_real_escape_string(
$db
,
$_POST
[
'password_2'
]);
19.
20.
if
(mysqli_connect_errno())
21.
{
22.
echo
"Database Connect Failed : "
. mysqli_connect_error();
23.
exit
();
24.
}
25.
26.
27.
$intRejectTime
= 20;
28.
$sql
=
"UPDATE member SET LoginStatus = '0', LastUpdate = '0000-00-00 00:00:00' WHERE 1 AND DATE_ADD(LastUpdate, INTERVAL $intRejectTime MINUTE) <= NOW() "
;
29.
$query
= mysqli_query(
$con
,
$sql
);
30.
31.
32.
if
(
empty
(
$username
)) {
array_push
(
$errors
,
"Username is required"
); }
33.
if
(
empty
(
$email
)) {
array_push
(
$errors
,
"Email is required"
); }
34.
if
(
empty
(
$password_1
)) {
array_push
(
$errors
,
"Password is required"
); }
35.
if
(
$password_1
!=
$password_2
) {
36.
array_push
(
$errors
,
"The two passwords do not match"
);
37.
}
38.
39.
40.
41.
$user_check_query
=
"SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1"
;
42.
$result
= mysqli_query(
$db
,
$user_check_query
);
43.
$user
= mysqli_fetch_assoc(
$result
);
44.
45.
if
(
$user
) {
46.
if
(
$user
[
'username'
] ===
$username
) {
47.
array_push
(
$errors
,
"Username already exists"
);
48.
}
49.
50.
if
(
$user
[
'email'
] ===
$email
) {
51.
array_push
(
$errors
,
"email already exists"
);
52.
}
53.
}
54.
55.
56.
if
(
count
(
$errors
) == 0) {
57.
$password
= md5(
$password
);
58.
59.
$query
= "INSERT INTO users (username, email, password)
60.
VALUES(
'$username'
,
'$email'
,
'$password'
)";
61.
mysqli_query(
$db
,
$query
);
62.
$_SESSION
[
'username'
] =
$username
;
63.
$_SESSION
[
'success'
] =
"เข้าสู่ระบบสำเร๊จ"
;
64.
header(
'location: index.php'
);
65.
}
66.
}
67.
68.
69.
if
(isset(
$_POST
[
'login_user'
])) {
70.
$username
= mysqli_real_escape_string(
$db
,
$_POST
[
'username'
]);
71.
$password
= mysqli_real_escape_string(
$db
,
$_POST
[
'password'
]);
72.
73.
if
(
empty
(
$username
)) {
74.
array_push
(
$errors
,
"กรุณาใส่ Username"
);
75.
}
76.
if
(
empty
(
$password
)) {
77.
array_push
(
$errors
,
"กรุณาใส่ Password"
);
78.
}
79.
80.
if
(
count
(
$errors
) == 0) {
81.
$password
= md5(
$password
);
82.
$query
=
"SELECT * FROM users WHERE username='$username' AND password='$password'"
;
83.
$results
= mysqli_query(
$db
,
$query
);
84.
if
(mysqli_num_rows(
$results
) == 1) {
85.
$_SESSION
[
'username'
] =
$username
;
86.
$_SESSION
[
'success'
] =
"เข้าสู่ระบบสำเร๊จ"
;
87.
header(
'location: index.php'
);
88.
}
else
{
89.
array_push
(
$errors
,
"ชื่อผู้ใช้ หรือ รหัสผ่านไม่ถูกต้อง"
);
90.
}
91.
}
92.
}
93.
94.
?>