-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollerUserData.php
More file actions
171 lines (156 loc) · 6.57 KB
/
controllerUserData.php
File metadata and controls
171 lines (156 loc) · 6.57 KB
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
165
166
167
168
169
170
171
<?php
session_start();
require "connection.php";
$errors = array();
// if (!isset($_SESSION['username'])) {
// echo "<script>
// alert('Please Login First');
// window.location.href='login.php';
// </script>";
// exit();
// }
//if user signup button
if (isset($_POST['signup'])) {
if (count($errors) === 0) {
// $encpass = password_hash($password, PASSWORD_BCRYPT);
$encpass = $_POST["password"];
$code = rand(999999, 111111);
$status = "notverified";
$insert_data = "INSERT INTO `registration`(first_name,last_name,username,email,password,confirm_password,gender,dob,mobile_no,`image`,code, status) VALUES('" .$first_name."','".$last_name."','".$username."','".$email."','".$password."','".$confirm_password."','".$gender."','".$dob."','".$mobile_no."','".$filename."','".$code."', '".$status."')";
$data_check = mysqli_query($conn, $insert_data);
if ($data_check) {
$subject = "Email Verification Code";
$message = "Your verification code is $code";
$sender = "From: shahiprem7890@gmail.com";
if (mail($email, $subject, $message, $sender)) {
$info = "We've sent a verification code to your email - $email";
$_SESSION['info'] = $info;
$_SESSION['email'] = $email;
$_SESSION['password'] = $password;
header('location: user-otp.php');
exit();
} else {
$errors['otp-error'] = "Failed while sending code!";
}
} else {
$errors['db-error'] = "Failed while inserting data into database!";
}
}
}
//if user click verification code submit button
if (isset($_POST['check'])) {
$_SESSION['info'] = "";
$otp_code = mysqli_real_escape_string($conn, $_POST['otp']);
$check_code = "SELECT * FROM registration WHERE code = $otp_code";
$code_res = mysqli_query($conn, $check_code);
if (mysqli_num_rows($code_res) > 0) {
$fetch_data = mysqli_fetch_assoc($code_res);
$fetch_code = $fetch_data['code'];
$email = $fetch_data['email'];
$code = 0;
$status = 'verified';
$update_otp = "UPDATE registration SET code = $code, status = '$status' WHERE code = $fetch_code";
$update_res = mysqli_query($conn, $update_otp);
} else {
$errors['otp-error'] = "You've entered incorrect code!";
}
}
require 'plugin/PHPMailer/src/Exception.php';
require 'plugin/PHPMailer/src/PHPMailer.php';
require 'plugin/PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if (isset($_POST['check-email'])) {
$email = mysqli_real_escape_string($conn, $_POST['email']);
$check_email = "SELECT * FROM registration WHERE email='$email'";
$run_sql = mysqli_query($conn, $check_email);
if (mysqli_num_rows($run_sql) > 0) {
$code = rand(999999, 111111);
$insert_code = "UPDATE registration SET code = $code WHERE email = '$email'";
$run_query = mysqli_query($conn, $insert_code);
if ($run_query) {
$subject = "Password Reset Code";
$message = "Your password reset code is $code";
// Create an instance of PHPMailer
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'ayankamila005@gmail.com'; // Your email
$mail->Password = 'cqsi djuk fuis imdd'; // Your password
// Recipients
$mail->setFrom('ayankamila005@gmail.com', 'Clickbase E-Commerce');
$mail->addAddress($email); // Use the email from the form
// Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
// Send the email
$mail->send();
// Success message
$info = "We've sent a password reset OTP to your email - $email";
$_SESSION['info'] = $info;
$_SESSION['email'] = $email;
$_SESSION['code'] = $code;
header('location: reset-code.php');
exit();
} catch (Exception $e) {
$errors['otp-error'] = "Failed while sending code: {$mail->ErrorInfo}";
}
} else {
$errors['db-error'] = "Something went wrong!";
}
} else {
$errors['email'] = "This email address does not exist!";
}
}
//if user click check reset otp button
if (isset($_POST['check-reset-otp'])) {
$_SESSION['info'] = "";
$otp_code = mysqli_real_escape_string($conn, $_POST['otp']);
$check_code = "SELECT * FROM registration WHERE code = $otp_code";
$code_res = mysqli_query($conn, $check_code);
if (mysqli_num_rows($code_res) > 0) {
$fetch_data = mysqli_fetch_assoc($code_res);
$email = $fetch_data['email'];
$_SESSION['email'] = $email;
$info = "Please create a new password that you don't use on any other site.";
$_SESSION['info'] = $info;
header('location: new_password.php');
exit();
} else {
$errors['otp-error'] = "You've entered incorrect code!";
}
}
//if user click change password button
if (isset($_POST['change-password'])) {
$_SESSION['info'] = "";
$password = mysqli_real_escape_string($conn, $_POST['password']);
$cpassword = mysqli_real_escape_string($conn, $_POST['cpassword']);
if ($password !== $cpassword) {
$errors['password'] = "Confirm password not matched!";
} else {
$code = 0;
$email = $_SESSION['email']; //getting this email using session
// $encpass = password_hash($password, PASSWORD_BCRYPT);
$encpass = $_POST["password"];
$c_encpass = $_POST["cpassword"];
$update_pass = "UPDATE registration SET code = $code, password = '$encpass',confirm_password = '$c_encpass' WHERE email = '$email'";
$run_query = mysqli_query($conn, $update_pass);
if ($run_query) {
$info = "Your password changed. Now you can login with your new password.";
$_SESSION['info'] = $info;
header('Location: password_changed.php');
} else {
$errors['db-error'] = "Failed to change your password!";
}
}
}
//if login now button click
if (isset($_POST['login-now'])) {
header('Location: login.php');
}