Website-Redesign/static/guestbook/submit.php

77 lines
1.5 KiB
PHP

<?php
class data {
public $name;
public $message;
public $date;
public $mlength;
public $nlength;
function set_date() {
$this->date = date("Y M d");
}
function set_mlength() {
$this->mlength = strlen($this->message);
}
function set_nlength() {
$this->nlength = strlen($this->name);
}
}
$ndata = new data();
$name = $ndata->name = htmlspecialchars($_POST["fname"]);
$message = $ndata->message = htmlspecialchars($_POST["fmessage"]);
$ndata->set_date();
$ndata->set_mlength();
$ndata->set_nlength();
$mlength = $ndata->mlength;
$nlength = $ndata->nlength;
# Setup JSON
$newURL = "https://" . $_SERVER['SERVER_NAME'] . "/guestbook/index.html";
# Check name and message
if ($name == "") {
header('Refresh:2; url=' . $newURL );
echo "There is no name, Try again with a name";
exit();
}
if ($message == "") {
header('Refresh:2; url=' . $newURL );
echo "There is no message, Try again with a message";
exit();
}
if($mlength > 200) {
header('Refresh:2; url=' . $newURL );
echo "This message is too long";
exit();
}
if($nlength > 25 ) {
header('Refresh:2; url=' . $newURL );
echo "The name is too long";
exit();
}
$data = json_decode(file_get_contents('test.json'), true);
foreach($data as $post) {
if ($post["message"] == $message) {
echo "Another message with this text already exists";
header('Refresh:2; url=' . $newURL );
exit();
}
}
array_unshift($data , $ndata);
file_put_contents('test.json', json_encode($data, JSON_PRETTY_PRINT));
header('Refresh:2; url=' . $newURL );
echo "Your submisson has been added";
exit();