-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregistration.php
164 lines (133 loc) · 5.82 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
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
<?php include 'header.php';?>
<?php
$name = $email = $password = $gradYear = $major = $occupation = $profilePicture = "";
$nameErr = $emailErr = $passwordErr= $gradYearErr = $majorErr = $occupationErr = $profilePictureErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate and sanitize name
if (empty($_POST['name'])) {
$nameErr = 'Name is required';
} else {
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
}
// Validate and sanitize email
if (empty($_POST['email'])) {
$emailErr = 'Email is required';
} else {
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = 'Invalid email format';
}
}
// Check if the user already exists based on the email
$check_user_query = "SELECT * FROM alumni_table WHERE email = '$email'";
$check_user_result = mysqli_query($conn, $check_user_query);
if ($check_user_result && mysqli_num_rows($check_user_result) > 0) {
$emailErr = 'User with this email already exists';
echo $emailErr;
}
// Validate and sanitize password
if (empty($_POST['password'])) {
$passwordErr = 'Password is required';
} else {
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
$hashed_password = password_hash($password,PASSWORD_DEFAULT);
}
// Validate and sanitize graduation year
if (empty($_POST['gradYear'])) {
$gradYearErr = 'Graduation year is required';
} else {
$gradYear = filter_input(INPUT_POST, 'gradYear', FILTER_SANITIZE_NUMBER_INT);
}
// Validate and sanitize major
if (empty($_POST['major'])) {
$majorErr = 'Major is required';
} else {
$major = filter_input(INPUT_POST, 'major', FILTER_SANITIZE_SPECIAL_CHARS);
}
// Validate and sanitize occupation
if (empty($_POST['occupation'])) {
$occupationErr = 'Occupation is required';
} else {
$occupation = filter_input(INPUT_POST, 'occupation', FILTER_SANITIZE_SPECIAL_CHARS);
}
// Validate and handle profile picture upload
if (!empty($_FILES['profilePicture']['name'])) {
$targetDirectory = "uploads/";
$targetFile = $targetDirectory . basename($_FILES["profilePicture"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
// Check if the image file is an actual image or fake image
$check = getimagesize($_FILES["profilePicture"]["tmp_name"]);
if ($check !== false) {
$uploadOk = 1;
} else {
$profilePictureErr = "File is not an image.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["profilePicture"]["size"] > 500000) {
$profilePictureErr = "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow only certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
$profilePictureErr = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$profilePictureErr = "Sorry, your file was not uploaded.";
} else {
// Move the uploaded file to the desired directory
if (move_uploaded_file($_FILES["profilePicture"]["tmp_name"], $targetFile)) {
$profilePicture = $targetFile;
} else {
$profilePictureErr = "Sorry, there was an error uploading your file.";
}
}
}
// Check if there are no errors before processing the form
if (empty($nameErr) && empty($emailErr) && empty($gradYearErr) && empty($majorErr) && empty($occupationErr) && empty($profilePictureErr) && empty($passwordErr)){
// Insert data into the database
$sql = "INSERT INTO alumni_table (name, email,hashed_password,gradYear, major, occupation, profilePicture) VALUES ('$name', '$email', '$hashed_password','$gradYear', '$major', '$occupation', '$profilePicture')";
if (mysqli_query($conn, $sql)) {
// Success
// Redirect to the login.php file
header('Location: login.php');
exit();
} else {
echo 'Error: ' . mysqli_error($conn);
}
}
}
?>
<title>Alumni Registration</title>
<link rel="stylesheet" href="registration.css">
</head>
<body>
<div class="container">
<h1>Alumni Registration</h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
<label for="name">Full Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<label for="gradYear">Graduation Year:</label>
<input type="number" id="gradYear" name="gradYear" required>
<label for="major">Major:</label>
<input type="text" id="major" name="major" required>
<label for="occupation">Occupation:</label>
<input type="text" id="occupation" name="occupation" required>
<label for="profilePicture">Profile Picture:</label>
<input type="file" id="profilePicture" name="profilePicture" accept="image/*">
<button type="submit" name="registration">Register</button>
<p>Already Have an account <a href="/login.php">Login</a></p>
</form>
</div>
<?php include 'footer.php';?>
</body>
</html>
<?php
?>