-
Notifications
You must be signed in to change notification settings - Fork 3
/
registration.php
122 lines (102 loc) · 3.68 KB
/
registration.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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'includes/config.php';
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve form data
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirmPassword = $_POST['confirm_password'];
// Perform form validation
if (empty($name)) {
$errors[] = "Name is required";
}
if (empty($email)) {
$errors[] = "Email is required";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format";
}
if (empty($password)) {
$errors[] = "Password is required";
}
if ($password !== $confirmPassword) {
$errors[] = "Passwords do not match";
}
// If there are no validation errors, proceed with further processing
if (empty($errors)) {
// Hash the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Prepare the SQL statement
$stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $hashedPassword);
// Execute the statement
if ($stmt->execute()) {
// Registration successful
session_start();
$_SESSION['registration_success'] = true;
// Log out the user
unset($_SESSION['customer_id']);
// Redirect to the login page with recorded email
header("Location: login.php?email=" . urlencode($email));
exit;
} else {
$errors[] = "An error occurred while processing your request. Please try again later.";
}
// Close the statement and connection
$stmt->close();
$conn->close();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Customer Registration</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.container {
max-width: 500px;
margin-top: 50px;
}
.alert {
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1 class="mt-5">Registration</h1>
<?php if (isset($errors) && !empty($errors)) { ?>
<div class="alert alert-danger" role="alert">
<?php foreach ($errors as $error) {
echo $error . "<br>";
} ?>
</div>
<?php } ?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="form-group">
<label for="confirm_password">Confirm Password</label>
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
</body>
</html>