-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_signup.php
95 lines (81 loc) · 2.94 KB
/
process_signup.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
<?php
require_once('config.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Maida - Sign Up</title>
</head>
<body>
<!-- CDN for SweetAlert: Shows an alert when the user registers successfully -->
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<!-- CDN for JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/cesiumjs/1.78/Build/Cesium/Cesium.js"></script>
<?php
if(isset($_POST['submit'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$error = false;
if(empty($username) | empty($email) | empty($password)){
$error = true;
header("Location: signup.php?error=empty");
exit();
}
if(!preg_match("/^[a-zA-Z][a-zA-Z0-9_]*[a-zA-Z0-9]@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/", $email)){
$error = true;
header("Location: signup.php?error=invalidemail&username=$username");
exit();
}
if(strlen($password) < 6){
$error = true;
header("Location: signup.php?error=shortpassword&username=$username");
exit();
}
$sql = "SELECT * FROM users WHERE username = :username";
$stmt = $db->prepare($sql);
$stmt->execute(['username' => $username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if($user){
$error = true;
header("Location: signup.php?error=usernametaken&email=$email");
exit();
}
// check if email is already taken with pdo
$sql = "SELECT * FROM users WHERE email = :email";
$stmt = $db->prepare($sql);
$stmt->execute(['email' => $email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if($user){
$error = true;
header("Location: signup.php?error=emailtaken&username=$username");
exit();
}
if(!$error){
$password = sha1($password);
// insert into database
$sql = "INSERT INTO users (username, password, email, address, district, city, phone) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $db->prepare($sql);
$result = $stmt -> execute([$username, $password, $email, "", "", "", ""]);
if($result){
print("<script>
Swal.fire({
title: 'Success!',
text: 'You have successfully registered!',
icon: 'success',
confirmButtonText: 'OK'
}).then(function() {
window.location = 'preauth.php';
});
</script>");
}else{
print("<p>Error in registering, sorry!</p>");
}
}
}
?>
</body>
</html>