stories/app/submit/addComment.php

50 lines
1.9 KiB
PHP
Executable File

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include_once("../includes/config.php");
function generateRandomString($length = 11) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$commentUrlGen = generateRandomString();
// check if request is post
if(isset($_POST["commentSubmitButton"])) {
$postUrl = htmlspecialchars(strip_tags($_POST["postUrl"], ENT_QUOTES));
$author = htmlspecialchars(strip_tags($_POST["commentAuthor"], ENT_QUOTES));
$commentContent = htmlspecialchars(strip_tags($_POST["commentText"], ENT_QUOTES));
// check if comment content is empty
if($commentContent == "") {
echo "<p>Your comment is empty.</p>";
}
else {
if(!$author) {
$author = "Anonymous";
}
// insert comment into database
$query = $con->prepare("INSERT INTO comments(comment_url, comment_author, comment_body, comment_onPost) VALUES(:commentUrl, :commentAuthor, :commentContent, :commentOnPost)");
$query->bindParam(":commentUrl", $commentUrlGen);
$query->bindParam(":commentContent", $commentContent);
$query->bindParam(":commentAuthor", $author);
$query->bindParam(":commentOnPost", $postUrl);
$query->execute();
// redirect to post page
header("Location: ../status.php?url=$postUrl");
}
} else {
echo "<p>This is not a post request</p>";
}
?>