-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrationGuest.php
143 lines (123 loc) · 3.47 KB
/
registrationGuest.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
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "vendor/autoload.php";
//include_once 'db_connection.php';
/*
$sql = "SELECT Email FROM Users";
$email_check = $conn->query($sql);
$isTakenG = false;
$name = $email = "";
$nameErrG = $emailErrG = "";
$countOfSuccesfulFieldsG = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
session_start();
*/
// Name
if (empty($_POST["name"])) {
$nameErrG = "First name is required";
} else {
$name = test_input($_POST["name"]);
//$_SESSION["inputNameG"] = $name;
$countOfSuccesfulFieldsG ++;
}
// EMAIL
if (empty($_POST["email"])) {
$emailErrG = "Email is required";
} else if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {/*copied from www.w3school.com */
$emailErrG = "Invalid email format";
}
else{
$email = test_input($_POST["email"]);
while($row = $email_check->fetch_assoc())
{
if($row["Email"] == $email)
$isTakenG = true;
}
if($isTakenG)
{
$emailErrG = "Email has already been taken";
}
else
{
//$_SESSION["inputEmailG"] = $email;
$countOfSuccesfulFieldsG ++;
}
}
if($countOfSuccesfulFieldsG == 2)
{
$pass = randomPassword();
print $pass;
$msg = "Just in case you ever lost something here is your password: \n"
. $pass;
$mail = new PHPMailer(TRUE);
try {
$mail->setFrom('[email protected]', 'Lost & Found');
$mail->addAddress($email, "Dear " . $name);
$mail->Subject = 'Your password for Lost & Found';
$mail->Body = $msg;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = '[email protected]';
$mail->Password = 'lost&found';
$mail->Port = 587;
/* Disable some SSL checks. */
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
/* Enable SMTP debug output. */
//$mail->SMTPDebug = 4;
$mail->send();
}
catch (Exception $e)
{
echo $e->errorMessage();
}
catch (\Exception $e)
{
echo $e->getMessage();
}
//mail($email, "Your password for Lost & Found", $msg);
$hashPass = password_hash($pass, PASSWORD_DEFAULT);
$insertQuery = "INSERT INTO Users (Name, Email, Pass)
VALUES ('{$name}', '{$email}', '{$hashPass}')";
//session_start();
$_SESSION['loggedIn'] = 0;
if($conn->query($insertQuery) === TRUE)
{
//echo "New record created successfully";
$_SESSION['id'] = $conn->insert_id;
$_SESSION['loggedIn'] = 1;
$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
header('Location: index.php');
}
else
echo "Error";
}//if
/*
}//if
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
*/
function randomPassword() {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
?>