-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollerUserData.php
99 lines (89 loc) · 3.63 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
<?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']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);
if($password !== $cpassword){
$errors['password'] = "Confirm password not matched!";
}
$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){
$encpass = password_hash($password, PASSWORD_BCRYPT);
$insert_data = "INSERT INTO usertable (name, email, password, status)
values('$name', '$email', '$encpass', '$status')";
$data_check = mysqli_query($con, $insert_data);
if($data_check){
header('location: login-user.php');
}else{
$errors['db-error'] = "Failed, Try again";
}
}
}
//if user click login button
if(isset($_POST['login'])){
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$check_email = "SELECT * FROM usertable WHERE email = '$email'";
$res = mysqli_query($con, $check_email);
if(mysqli_num_rows($res) > 0){
$fetch = mysqli_fetch_assoc($res);
$fetch_pass = $fetch['password'];
if(password_verify($password, $fetch_pass)){
header('location: index.html');
$_SESSION['email']=$email;
}else{
$errors['email'] = "Incorrect email or password!";
}
}else{
$errors['email'] = "It's look like you're not yet a member! Click on the bottom link to signup.";
}
}
//if user click continue button in forgot password form
if(isset($_POST['check-email'])){
$_SESSION['myemail']=$email;
$email = mysqli_real_escape_string($con, $_POST['email']);
$check_email = "SELECT * FROM usertable WHERE email='$email'";
$run_sql = mysqli_query($con, $check_email);
if(mysqli_num_rows($run_sql) > 0){
header('location: new-password.php');
}else{
$errors['email'] = "This email address does not exist!";
}
}
//if user click change password button
if(isset($_POST['change-password'])){
$_SESSION['info'] = "";
$email=$_SESSION['myemail'];
$password = mysqli_real_escape_string($con, $_POST['password']);
$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);
if($password !== $cpassword){
$errors['password'] = "Confirm password not matched!";
}else{
$encpass = password_hash($password, PASSWORD_BCRYPT);
$update_pass = "UPDATE usertable SET password = '$encpass' WHERE email = '$email'";
$run_query = mysqli_query($con, $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-user.php');
}
?>