-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.php
135 lines (115 loc) · 5.11 KB
/
server.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
<?php
/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
session_start();
$message = "";
$username = "";
$email = "";
$errors = array();
$db = mysqli_connect('localhost', 'root', 'abiit@2019', 'rconnect');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$all_users = mysqli_query($db, "SELECT COUNT(*) AS COUNT FROM userData;");
$count = $all_users->fetch_assoc();
$user_count = $count['COUNT'];
if(isset($_POST['reg_user'])){
/*function email_validation($str) {
return (!preg_match(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $str))
? FALSE : TRUE;
}*/
$username = mysqli_real_escape_string($db, $_POST['reg_username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password']);
$password_2 = mysqli_real_escape_string($db, $_POST['passwordCon']);
$name = mysqli_real_escape_string($db, $_POST['reg_name']);
$enrl = mysqli_real_escape_string($db, $_POST['enrl']);
$branch_y = mysqli_real_escape_string($db, $_POST['branch_y']);
$bhawan = mysqli_real_escape_string($db, $_POST['bhawan']);
$bio = "Hey! I am ";
/*if (empty($username)) { array_push($errors, "Username is required"); $no_username = "Username is required"; }
if (empty($email)) { array_push($errors, "Email is required"); $no_email = "Email is required";}
if (empty($name)) { array_push($errors, "Name is required"); $no_name = "Name is required";}
if (empty($enrl)) { array_push($errors, "Enrl. No. is required"); $no_enrl = "Enrl. No. is required"; }
if (empty($bhawan)) { array_push($errors, "Bhawan is required"); $no_bhawan = "Bhawan is required";}
if (empty($password_1)) { array_push($errors, "Password is required"); $no_password = "Password is required";}
if (empty($branch_y)) { array_push($errors, "Branch/Year Required");}
if ($password_1 != $password_2) {array_push($errors, "The two passwords do not match"); $password_mismatch = "The two passwords do not match"; }
//regex check for e-mail
if(!email_validation($email)){array_push($errors, "Enter valid E-mail ID!");}
if(strlen($enrl) != 8){array_push($errors, "Enter valid ENRL. NO.");}*/
$user_check_query = "SELECT * from userData where username = '$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
if (count($errors) == 0) {
$hashed_password = password_hash($password_1, PASSWORD_BCRYPT);
$query = "INSERT INTO userData (name, enrl, bhawan, username, email, password, bio, branch_y)
VALUES('$name','$enrl','$bhawan','$username', '$email', '$hashed_password', '$bio$name', '$branch_y');";
if(mysqli_query($db, $query)){
echo "Success!";
}
$get_user = "SELECT * FROM userData WHERE username='$username'";
$result = mysqli_query($db, $get_user);
if(mysqli_num_rows($result) == 1){
while($row = mysqli_fetch_array($result)){
$_SESSION['id'] = $row['userID'];
$_SESSION['bhawan'] = $row['bhawan'];
$_SESSION['name'] = $row['name'];
$_SESSION['img'] = $row['img'];
$_SESSION['bio'] = $row['bio'];
$_SESSION['username'] = $row['username'];
}
}
}
}
if(isset($_POST['login_user'])){
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
//password_matching_code
$get_hash = mysqli_query($db, "SELECT password from userData where username='$username'");
$user_pass = $get_hash->fetch_assoc();
if(password_verify($password, $user_pass['password'])){
echo "1";
$query = "SELECT * FROM userData WHERE username='$username'";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1){
while($row = mysqli_fetch_array($result)){
$_SESSION['id'] = $row['userID'];
$_SESSION['bhawan'] = $row['bhawan'];
$_SESSION['name'] = $row['name'];
$_SESSION['img'] = $row['img'];
$_SESSION['bio'] = $row['bio'];
$_SESSION['username'] = $row['username'];
}
$id = $_SESSION['id'];
$fetch_last_activity = mysqli_query($db, "SELECT * from login_details where userID =$id");
if($fetch_last_activity->num_rows == 0){
$last_activity = "INSERT INTO login_details (userID, last_activity) VALUES ($id, NOW())";
mysqli_query($db, $last_activity);
}
else{
$last_activity = "UPDATE login_details SET last_activity = NOW() where userID = $id";
mysqli_query($db, $last_activity);
}
$_SESSION['username'] = $username;
$_SESSION['success'] = 1;
//header('location: welcome.php');
}
}
else{
array_push($errors, "1");
echo "Error!";
}
}
?>