-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.php
112 lines (86 loc) · 2.98 KB
/
signup.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
<?php
// Starting the session, necessary
// for using session variables
session_start();
// Declaring and hoisting the variables
$username = "";
$email = "";
$errors = array();
$_SESSION['success'] = "";
// DBMS connection code -> hostname,
// username, password, database name
$db = mysqli_connect('localhost', 'root', '', 'website');
// Registration code
if (isset($_POST['submit'])) {
// Receiving the values entered and storing
// in the variables
// Data sanitization is done to prevent
// SQL injections
$username = mysqli_real_escape_string($db, $_POST['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['cpassword']);
// Ensuring that the user has not left any input field blank
// error messages will be displayed for every blank input
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
// Checking if the passwords match
}
// If the form is error free, then register the user
if (count($errors) == 0) {
// Password encryption to increase data security
$password = md5($password_1);
// Inserting data into table
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
// Storing username of the logged in user,
// in the session variable
$_SESSION['Email'] = $username;
// Welcome message
$_SESSION['success'] = "You have logged in";
// Page on which the user will be
// redirected after logging in
header('location: login.php');
}
}
// User login
if (isset($_POST['log'])) {
// Data sanitization to prevent SQL injection
$username = mysqli_real_escape_string($db, $_POST['Email']);
$password = mysqli_real_escape_string($db, $_POST['password']);
// Error message if the input field is left blank
if (empty($username)) {
array_push($errors, "Email is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// Checking for the errors
if (count($errors) == 0) {
// Password matching
$password = md5($password);
$query = "SELECT * FROM users WHERE Email=
'$username' AND password='$password'";
$results = mysqli_query($db, $query);
// $results = 1 means that one user with the
// entered username exists
if (mysqli_num_rows($results) == 1) {
// Storing username in session variable
$_SESSION['Email'] = $username;
// Welcome message
$_SESSION['success'] = "You have logged in!";
// Page on which the user is sent
// to after logging in
header('location: welcome.php');
}
else {
// If the username and password doesn't match
array_push($errors, "Email or password incorrect");
}
}
}
?>