-
Notifications
You must be signed in to change notification settings - Fork 93
/
validate.php
212 lines (173 loc) · 9.34 KB
/
validate.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
if(isset($_POST['register'])){
if(!isset($_SESSION))
session_start();
include('connection.php');
$username = mysqli_real_escape_string($conn,trim($_POST['username']));
$mobile_number = mysqli_real_escape_string($conn,trim($_POST['mobile_number']));
$email_address = mysqli_real_escape_string($conn,trim($_POST['email_address']));
$password = mysqli_real_escape_string($conn,trim($_POST['password']));
$confirm_password = mysqli_real_escape_string($conn,trim($_POST['confirm_password']));
if($password == $confirm_password){
if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/",$email_address)){
if(preg_match("/^(\+?\d{1,4})?[-.\s]?(\()?(\d{1,3})(?(2)\))[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/", $mobile_number)){
$sql_email = "SELECT email_address FROM user WHERE email_address='$email_address'";
$result_email = mysqli_query($conn,$sql_email);
$sql_mobile = "SELECT mobile_number FROM user WHERE mobile_number='$mobile_number'";
$result_mobile = mysqli_query($conn,$sql_mobile);
if(mysqli_num_rows($result_email)>0){
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Oops...","Email Address '. $email_address.' is already exists!","error");';
echo '}, 500);</script>';
}
else if(mysqli_num_rows($result_mobile)>0){
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Oops...","Mobile number '.$mobile_number.' is already exists!","error");';
echo '}, 500);</script>';
}else{
$activation_code = hash('sha256',mt_rand(0,1000));
$hash_password = md5($password);
$sql = "INSERT INTO user (`username`,`password`,`mobile_number`,`email_address`,`activation_code`) VALUES('$username','$hash_password','$mobile_number','$email_address','$activation_code')";
$result = mysqli_query($conn,$sql);
if(!$result)
die("Error while updating!!!...").mysqli_error($conn);
else{
$_SESSION['username']=$username;
$_SESSION['mobile_number']=$mobile_number;
$_SESSION['email_address']=$email_address;
$_SESSION['password']=$password;
$_SESSION['activation_code']=$activation_code;
include('activate_email.php');
}
}
}else{
//invalid mobile number error message
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Oops...","Mobile number '. $mobile_number.' is invalid!","error");';
echo '}, 500);</script>';
}
}else{
//email address invalid error messaage
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Oops...","Email address '. $email_address.' is invalid!","error");';
echo '}, 500);</script>';
}
}else{
//password does not match error
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Oops...","The two passwords does not match!","error");';
echo '}, 500);</script>';
}
}
if(isset($_POST['login'])){
session_start();
include('connection.php');
$email_address = mysqli_real_escape_string($conn,$_POST['email_address']);
$password = mysqli_real_escape_string($conn,$_POST['password']);
$hash_password = md5($password);
$sql = "SELECT * FROM user WHERE email_address = '$email_address' AND password = '$hash_password' ";
$result=mysqli_query($conn,$sql);
if(!$result){
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Warning...","Error while loggin in!..","warning");';
echo '}, 500);</script>';
}
else
{
$row=mysqli_fetch_array($result);
$username = $row['username'];
$count=mysqli_num_rows($result);
if($count==1)
{
if($row['confirm_status']==0)
{
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Warning...","Please activate your account first!..","warning");';
echo '}, 500);</script>';
}else{
if($row['email_address'] == 'admin@gmail.com' && $row['password'] == 'c12b240b5710c6c9ee00ef4529803aac'){
$_SESSION['username']=$username;
$_SESSION['email_address'] = $email_address;
header('location:admin_page.php');
}else{
$_SESSION['username']=$username;
$_SESSION['email_address'] = $email_address;
header('location:profile.php');
}
}
}else{
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Oops...","Wrong username or Password!...","error");';
echo '}, 500);</script>';
}
}
}
if(isset($_POST['forgot'])){
session_start();
include('connection.php');
$email_address = mysqli_real_escape_string($conn,$_POST['email_address']);
$sql = "SELECT email_address FROM user WHERE email_address = '$email_address' ";
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result)==0){
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Oops...","Email address '. $email_address.' does not exists!","error");';
echo '}, 500);</script>';
}else{
require 'phpmailer/PHPMailerAutoload.php';
$_SESSION['email_address'] = $email_address;
$sql = "SELECT username FROM user WHERE email_address = '$email_address' ";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
$username = $row['username'];
$_SESSION['username'] = $username;
$mail = new PHPMailer;
// $mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your email address'; // SMTP username
$mail->Password = 'smtp password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$to=$email_address;
$mail->setFrom('your email address', 'Musical World');
$mail->addAddress($to); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Reset Your Account';
$mail->Body = "
<br><br>
<b>Hello,".$username.",</b><br>
You recently requested to reset your password for your Musical World account.Click the button below to reset it. <br>
<br><br>
<button type='button' class='btn btn-secondary'><a href='http://localhost/musical_world/reset_password.php'>Reset Password</a></button>
<br><br>
If you did not requested to reset your password please ignore this email or reply to let us know.<br><br>
<pre>Thanks,
<b>Musical World</b></pre>";
if(!$mail->send()){
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Oops..."," Error while sending email.Please check your your internet coonection!","error");';
echo '}, 500);</script>';
}else{
echo '<script type="text/javascript">';
echo 'setTimeout(function () { sweetAlert("Success","A link have been sent to your email aadress '.$to.' Please reset your Password by pressing the link.","success");';
echo '}, 500);</script>';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="KEERTHANA KUTEERA LOGO-BLACK-01.png" type="image/png">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@7.28.11/dist/sweetalert2.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@7.28.11/dist/sweetalert2.min.js"></script>
</body>
</html>