-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_connect
More file actions
202 lines (141 loc) · 5.88 KB
/
php_connect
File metadata and controls
202 lines (141 loc) · 5.88 KB
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
<?php
session_start();
$con = mysqli_connect("localhost", "root", "", "social");
if(mysqli_connect_errno()) {
echo "failed to connect: " . mysqli_connect_errno();
}
//Declearing variables to prevent errors
$fname = "";
$lname = "";
$em = "";
$em2 = "";
$password = "";
$password2 = "";
$date = "";
$profile_pic = "";
$error_array = array();
if (isset($_POST['register_button'])) {
$fname = strip_tags($_POST['reg_fname']); // remove html tags
$fname = str_replace(' ', '', $fname); //remove spaces
$fname = ucfirst(strtolower($fname));
$_SESSION['reg_fname'] = $fname; //Stores name into session variable
$lname = strip_tags($_POST['reg_lname']); // remove html tags
$lname = str_replace(' ', '', $lname); //remove spaces
$lname = ucfirst(strtolower($lname));
$_SESSION['reg_lname'] = $lname; //Stores name into session variable
$em = strip_tags($_POST['reg_email']); // remove html tags
$em = str_replace(' ', '', $em); //remove spaces
$em = ucfirst(strtolower($em));
$_SESSION['reg_email'] = $em; //Stores name into session variable
$em2 = strip_tags($_POST['reg_email2']); // remove html tags
$em2 = str_replace(' ', '', $em2); //remove spaces
$em2 = ucfirst(strtolower($em2));
$_SESSION['reg_email2'] = $em2; //Stores name into session variable
$password = strip_tags($_POST['reg_password']); // remove html tags
$password2 = strip_tags($_POST['reg_password2']); // remove html tags
$date = date("Y-m-d");
//check if email is in valid format
if ($em == $em2) {
//check if email is in valid format
if(filter_var($em, FILTER_VALIDATE_EMAIL)){
$em = filter_var($em, FILTER_VALIDATE_EMAIL);
//check if email already exist
$e_check = mysqli_query($con, "SELECT email FROM users WHERE email ='$em'");
//count the number of rows returm
$num_rows = mysqli_num_rows($e_check);
if ($num_rows > 0) {
array_push($error_array, "Email already in use<br>");
}
}
else {
array_push($error_array, "Invalid email format<br>");
}
}
else {
array_push($error_array, "Email don't match<br>");
}
if (strlen($fname) > 25 || strlen($fname) < 2) {
array_push($error_array, "your first name must be between 2 and 25 characters");
}
if (strlen($lname) > 25 || strlen($lname) < 2) {
array_push($error_array, "your last name must be between 2 and 25 characters");
}
if ($password != $password2) {
array_push($error_array, "Your passwords do not match");
}
else
{
if (preg_match('/[^A-Za-z0-9]/', $password)) {
array_push($error_array, "Your password can only contain english characters or numbers");
}
}
if (strlen($password > 30 || strlen($password) < 5)) {
array_push($error_array, "Your password must be between 5 and 30 characters");
}
if (empty($error_array)){
$password = md5($_password); // encrypt password before sending to database
//Generate username by concatinating firstname and last name
$username = strtolower($fname . "_" .$lname);
$check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username='$username'");
$i = 0;
// if usernme exists add number to username
while(mysqli_num_rows($check_username_query) != 0){
$i++;//add one to i
$username = username . "_" .$i;
$check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username='$username'");
}
//profile picture assignment
$rand = rand(1, 2) //random number between 1 and two
($rand == 1)
$profile_pic = "beach.jpeg";
/// Need to ad more photos for users to get by random when sighning up
else ($rand == 2)
$profile_pic = "beach.jpeg";
$query = mysqli_query($con, "INSERT INTO users VALUES ('', '$fname', '$lname', '$username', '$date', 'profile_pic', '0', '0', 'no', ',')");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to ADDVERT</title>
</head>
<body>
<div class="login_form">
<form action="register.php" method="POST">
<input type="text" name="reg_fname" placeholder="First Name" value=" <?php if(isset($_SESSION['reg_fname'])){
echo $_SESSION['reg_fname'];
}?>" required>
<br>
<?php if(in_array("your first name must be between 2 and 25 charactrs<br>", $error_array)) echo "your first name must be between 2 and 25 characters<br>";?>
<input type="text" name="reg_lname" placeholder="Last Name" value=" <?php if(isset($_SESSION['reg_lname'])){
echo $_SESSION['reg_lname'];
}?>"required>
<br>
<?php if(in_array("your last name must be between 2 and 25 charactrs<br>", $error_array)) echo "your last name must be between 2 and 25 characters<br>";?>
<input type="email" name="reg_email" placeholder="Email" value=" <?php if(isset($_SESSION['reg_email'])){
echo $_SESSION['reg_email'];
}?>"required>
<br>
<?php if(in_array("Invalid email format<br>", $error_array)) echo "Invalid email format<br>";?>
<input type="email" name="reg_email2" placeholder="Confirm Email" value=" <?php if(isset($_SESSION['reg_email2'])){
echo $_SESSION['reg_email2'];
}?>" required>
<br>
<?php if(in_array("Email already in use<br>", $error_array)) echo "Email already in use<br>";
else if(in_array("Invalid email format<br>", $error_array)) echo "Invalid email format<br>";
else if(in_array("Email don't match<br>", $error_array)) echo "Email don't match<br>";
?>
<input type="password" name="reg_password" placeholder="Confirm Password" required>
<br>
<input type="password" name="reg_password2" placeholder="Confirm Password" required>
<br>
<?php if(in_array("Your passwords do not match<br>", $error_array)) echo "Your passwords do not match<br>";
else if(in_array("Your password can only contain english characters or numbers<br>", $error_array)) echo "Your password can only contain english charactors or numbers<br>";
else if(in_array("Your password must be between 5 and 30 characters<br>", $error_array)) echo "Your password must be between 5 and 30 characters<br>";
?>
<input type="submit" name="register_button" value="Rgister">
</form>
</div>
</body>
</html>