I was adding edit functionality to a comment system that i was creating and i ran into an issue. I know that my code is quite vulnerable, but thats the way i want to create it! For now atleast :D
I have the edit form on page 'comments.inc.php'
echo "<form class='edit-form' method='POST' action='editcomment.php'>
<input type='hidden' name='cid' value='".$row['cid']."'>
<input type='hidden' name='uid' value='".$row['uid']."'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<input type='hidden' name='message' value='".$row['message']."'>
<button name='editSubmit'>Edit</button>
</form>";
I have another page 'editcomment.php' which looks like this
$cid = $_POST['cid'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
echo "<form method='POST' action='".editComments($conn)."'>
<input type='hidden' name='cid' value='".$cid."'>
<input type='hidden' name='uid' value='".$uid."'>
<input type='hidden' name='date' value='".$date."'>
<textarea name='message'>".$message." ".$_POST['editSubmit']."</textarea>
<br>
<button name='editCommentSubmit'>Edit</button>
</form>";
The editComments($conn) function is again on 'comments.inc.php' and looks like this
function editComments($conn) {
if (isset($_POST['editCommentSubmit'])) {
$cid = $_POST['cid'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "UPDATE comments SET message='$message', date='$date' WHERE cid='$cid'";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
}
}
This code works as expected but I wanted to restrict unauthorized access of 'editcomment.php' page, so i modified my 'editcomment.php' page like this
if (isset($_POST['editSubmit'])) {
$cid = $_POST['cid'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
echo "<form method='POST' action='".editComments($conn)."'>
<input type='hidden' name='cid' value='".$cid."'>
<input type='hidden' name='uid' value='".$uid."'>
<input type='hidden' name='date' value='".$date."'>
<textarea name='message'>".$message."</textarea><br>
<button name='editCommentSubmit'>Edit</button>
</form>";
}
else {
header("Location: index.php?access=denied");
exit();
}
But for some reason it doesn't work anymore, it keeps redirecting me to index.php?access=denied. Please Help me :(