-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_registration.php
126 lines (98 loc) · 5.01 KB
/
process_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
<?php
require 'config.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$parts = explode('@', $email);
$roll_number_parts = explode('_', $parts[0]);
$roll_number = $roll_number_parts[1];
// Validate email address format
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_msg = "Invalid email address format";
} else {
// Check if email already exists in database
$sql = "SELECT * FROM student_login WHERE email = '$email'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$error_msg = "Email address already registered";
} else {
// Check if password and confirm password match
if ($password != $confirm_password) {
$error_msg = "Passwords do not match";
} else {
// Generate verification token
$verification_token = bin2hex(random_bytes(32));
// Insert user data into database
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO student_login (email, roll_number, first_name, last_name, password, verification_token) VALUES ('$email', '$roll_number', '$first_name', '$last_name', '$hashed_password', '$verification_token')";
$sql2 = "INSERT INTO student_details (roll_number) VALUES ('$roll_number');";
$sql3 = "INSERT INTO student_placement (roll_number) VALUES ('$roll_number')";
if (mysqli_query($conn, $sql) && mysqli_query($conn, $sql2) && mysqli_query($conn,$sql3))
{
echo "<script>alert('Please verify your email to finish the registration process');</script>";
$mail = new PHPMailer(true);
//$mail->SMTPDebug=3;
//SMTP settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'bxebezfpilbbvfna';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML(true);
//Sender and recipient information
$mail->setFrom('[email protected]');
$mail->addAddress($email);
$url = 'http://localhost/miniproject_dbms/verify_email.php?token='.$verification_token;
$message = 'Please click the following link to verify your email address:'.$url;
//Email content
$mail->Subject = "Verify your email address";
$mail->Body = "$message";
try
{
//Send the email
$mail->send();
echo "<script>window.location='student_login.html'</script>";
}
catch (Exception $e)
{
//Handle any exceptions that are thrown
echo "<script>alert('Message could not be sent. Error: {$mail->ErrorInfo}');</script>";
}
// Send verification email
// $to = $email;
// $subject = "Verify your email address";
// $message = "Please click the following link to verify your email address: http://localhost/TPC/student/verify_email.php?token=$verification_token";
// $headers = "From: verifier <[email protected]>\r\n";
// $headers .= "Do not reply\r\n";
// $headers .= "Content-type: text/html\r\n";
// mail($to, $subject, $message, $headers);
// // Redirect to login page
// echo "<script>window.location='student_login.html'</script>";
// // header("Location: student_login.php");
// exit();
}
else {
$error_msg = "Error: " . mysqli_error($conn);
}
}
}
mysqli_close($conn);
if (isset($error_msg)) {
echo "<script>alert('$error_msg');</script>";
echo "<script>window.location='student_registration.html'</script>";
// header("Location : student_register.php");
exit();
}
}
}
?>