-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollerUserData.php
104 lines (92 loc) · 3.67 KB
/
controllerUserData.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
<?php
session_start();
require "connection.php";
$email = "";
$name = "";
$errors = array();
//if user signup button
if(isset($_POST['signup'])){
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$email_check = "SELECT * FROM usertable WHERE email = '$email'";
$res = mysqli_query($con, $email_check);
if(mysqli_num_rows($res) > 0){
$errors['email'] = "Email that you have entered is already exist!";
}
if(count($errors) === 0){
$code = rand(999999, 111111);
$status = "notverified";
$insert_data = "INSERT INTO usertable (name, email, code, status)
values('$name', '$email', '$code', '$status')";
$data_check = mysqli_query($con, $insert_data);
if($data_check){
$subject = "Email Verification Code";
$message = "Your verification code is $code";
$sender = "From: [email protected]";
if(mail($email, $subject, $message, $sender)){
$info = "We've sent a verification code to your email - $email";
$_SESSION['info'] = $info;
$_SESSION['email'] = $email;
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($con, $_POST['otp']);
$check_code = "SELECT * FROM usertable WHERE code = $otp_code";
$code_res = mysqli_query($con, $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 usertable SET code = $code, status = '$status' WHERE code = $fetch_code";
$update_res = mysqli_query($con, $update_otp);
if($update_res){
$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
header('location: home.php');
exit();
}else{
$errors['otp-error'] = "Failed while updating code!";
}
}else{
$errors['otp-error'] = "You've entered incorrect code!";
}
}
//if user click login button
if(isset($_POST['login'])){
$email = mysqli_real_escape_string($con, $_POST['email']);
$check_email = "SELECT * FROM usertable WHERE email = '$email'";
$res = mysqli_query($con, $check_email);
$fetch = mysqli_fetch_assoc($res);
if(mysqli_num_rows($res) > 0){
$_SESSION['email'] = $email;
$status = $fetch['status'];
if($status == 'verified'){
$_SESSION['email'] = $email;
header('location: home.php');
}else{
$info = "It's look like you haven't still verify your email - $email";
$_SESSION['info'] = $info;
header('location: user-otp.php');
}
}
else{
$errors['email'] = "It's look like you're not yet a member! Click on the bottom link to signup.";
}
}
//if login now button click
if(isset($_POST['login-now'])){
header('Location: login-user.php');
}
?>