-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadWriteFileTest.php
More file actions
208 lines (158 loc) · 5.44 KB
/
Copy pathreadWriteFileTest.php
File metadata and controls
208 lines (158 loc) · 5.44 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
203
204
205
206
207
208
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/gh/nicolaspanel/numjs@0.15.1/dist/numjs.min.js"></script>
<script src="https://unpkg.com/ml5@0.4.3/dist/ml5.min.js"></script>
<script type="text/javascript" src="http://js.leapmotion.com/leap-0.6.3.min.js"></script>
<script src="readWriteFileTest.js"></script>
<script src="https://cdn.jsdelivr.net/npm/p5@1.1.9/lib/p5.js"></script>
<!--JQuery-->
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<!--<script src="prepareToDraw.js"></script>-->
</head>
<body onload="DisplayList()">
<main>
<?php
//console_log function https://stackify.com/how-to-log-to-console-in-php/
function console_log($output, $with_script_tags = true) {
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) .
');';
if ($with_script_tags) {
$js_code = '<script>' . $js_code . '</script>';
}
echo $js_code;
}
print '<p>POST Array:</p><pre>';
print_r($_POST);
print '</pre>';
//process form when it is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//VARS
$dataIsClean = true;
//Sanitize data of username
$username = filter_var($_POST["f_username"], FILTER_SANITIZE_STRING);
//SERVER SIDE VALIDATION
if ($username == "") {
print '<p class="error">Please enter your username.</p>';
$dataIsClean = false;
}
if (empty($username)) {
print '<p class="error">Username Empty.</p>';
$dataIsClean = false;
}
//print
//print '<p>Username: ' . $username . '</p>';
//data to file
//If data clean,
if ($dataIsClean) {
//Establish var for duplicateFound
$duplicateFound = false;
/*https://www.w3schools.com/php/func_filesystem_readfile.asp*/
//Read file
$usernameList = file_get_contents("usernames.txt");
//print_r($usernameList);
//print '<p></p>';
//Make into list
$usernameList = preg_split("/\s+/", $usernameList, -1, PREG_SPLIT_NO_EMPTY);
//print_r($usernameList);
//Search list
/*echo '<p>SEARCHING:</p>';
print_r($username);
echo '<p>IN LIST:</p>';
print_r($usernameList);*/
//If username in array
if (array_search($username, $usernameList) !== false) {
//Match found.
echo '<p>Username already entered.</p>';
$duplicateFound = true;
}
//If not in array
else {
echo '<p>Adding new user:' . $username . '</p>';
echo '<br><br>';
}
/*//Scan list for stuff
for ($i = 0; $i < count($usernameList); $i++) {
print_r($usernameList[$i]);
print_r($username);
echo '<br>';
//If match found
if ($usernameList[$i] === $username) {
echo '<p>FOUND MATCH</p>';
$duplicateFound = true;
}
}*/
//If match was found, don't write
if ($duplicateFound) {
console_log("Duplicate Found, cannot write.");
}
//No match found
else {
//Write to file
//Attempt to open file
@ $fp = fopen("usernames.txt", 'a');
//On fail
if (!$fp) {
echo '<p><strong>Cannot generate message file</strong></p></body></html>';
exit;
}
//On success
else {
//Add newline
$username = "\n" . $username;
//write username to file
fwrite($fp, $username);
print '<p>Username added to file: ' . $username . '</p>';
//Redirect to main game
}
}
}
}
?>
<!--PREVENT REFRESH ON SUBMIT-->
<!--https://stackoverflow.com/questions/23507608/form-submission-without-page-refresh-->
<!--<script type="text/javascript">
$(document).ready(function () {
$('#myform').on('submit', function(e) {
e.preventDefault();
$.ajax({
url : $(this).attr('action') || window.location.pathname,
type: "POST",
data: $(this).serialize(),
success: function (data) {
$("#form_output").html(data);
},
error: function (jXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
});
</script>-->
<form id="myform" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Enter Userame: <input id="username" type="text" name="f_username" placeholder="name">
<input onclick="return SignIn();" type="submit">
<!--<button onclick="return SignIn();" id="button" type="submit">Sign in</button>-->
</form>
</main>
<ul id="users" style = "color:black">
<!--<li>user1</li>-->
</ul>
</body>
</html>
<!--<!DOCTYPE html>
<html>
<body>
<?php
/*if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
*/?>
</body>
</html>-->