Added code

This commit is contained in:
filip 2021-02-18 15:45:13 +01:00
commit 46979c3b5a
4 changed files with 73 additions and 0 deletions

26
index.php Normal file
View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="form">
<form action="send.php" method="get">
Nickname: <input type="text" name="userName"><br>
Message:<br>
<input type="text" name="userMessage"><br>
<input type="submit" value="Send">
</form>
</div>
</div id="messages">
<iframe src="messages.php" id="messages">
</div>
</body>
</html>

15
messages.php Normal file
View File

@ -0,0 +1,15 @@
<?php
$message_file = fopen("messages.csv", "r");
while (($messages = fgetcsv($message_file)) !== FALSE) {
if ($messages[0] == "") {
$userName = "anon";
} else {
$userName = $messages[0];
}
echo("<p style='color: red;'>" . $userName . " says:</p><p>" . $messages[1] . "</p><p style='color: red;'>At: " . $messages[2] . "</p><hr>");
}
?>
<script>
window.scrollTo(0,document.body.scrollHeight);
</script>
<?php return; ?>

21
send.php Normal file
View File

@ -0,0 +1,21 @@
<?php
//set error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if ($_GET["userMessage"] == "") {
return;
}
$dateToWrite = date("Y-m-d H:i:s");
$array = array($_GET["userName"], $_GET["userMessage"], $dateToWrite, microtime());
//open csv file
//format is "user name", "user message", "Y-m-d H:i:s", "microtime"
fputcsv(fopen("messages.csv", "a+"), $array);
header("Location: index.php");
exit();
?>

11
style.css Normal file
View File

@ -0,0 +1,11 @@
#messages {
height: 70vh;
width: 99vw;
border-width: 0px;
}
body {
border-width: 0px;
margin: 0px;
background-color: lightgreen;
}