guestbook/submit.php

77 lines
1.5 KiB
PHP
Raw Normal View History

2024-04-20 10:21:45 -05:00
<?php
2024-04-26 16:32:37 -05:00
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();
2024-04-20 12:45:21 -05:00
2024-04-20 12:45:21 -05:00
2024-04-26 16:32:37 -05:00
$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;
2024-04-20 12:45:21 -05:00
# Setup JSON
2024-04-26 16:56:30 -05:00
$newURL = "https://" . $_SERVER['SERVER_NAME'] . "/guestbook/index.html";
2024-04-22 08:44:13 -05:00
2024-04-26 16:32:37 -05:00
# Check name and message
if ($name == "") {
2024-04-22 08:44:13 -05:00
header('Refresh:2; url=' . $newURL );
echo "There is no name, Try again with a name";
exit();
}
2024-04-26 16:32:37 -05:00
if ($message == "") {
2024-04-22 08:44:13 -05:00
header('Refresh:2; url=' . $newURL );
echo "There is no message, Try again with a message";
exit();
}
2024-04-26 16:32:37 -05:00
if($mlength > 200) {
header('Refresh:2; url=' . $newURL );
echo "This message is too long";
exit();
}
2024-04-27 09:11:53 -05:00
if($nlength > 25 ) {
2024-04-26 16:32:37 -05:00
header('Refresh:2; url=' . $newURL );
echo "The name is too long";
exit();
}
2024-04-20 12:45:21 -05:00
2024-04-26 16:32:37 -05:00
$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();
}
}
2024-04-26 16:32:37 -05:00
array_unshift($data , $ndata);
2024-04-22 08:44:13 -05:00
file_put_contents('test.json', json_encode($data, JSON_PRETTY_PRINT));
header('Refresh:2; url=' . $newURL );
echo "Your submisson has been added";
exit();