Writing to a file in PHP to create a comment section issues

In honesty, I think you would be better off completing your learning on databases and using one.

If learning databases or any php, this is something to be wary of. If the DB tutorial is not using prepared statements, or is using mysql, as opposed to mysqli or PDO, then avoid it like the plague.
Which brings me to a few points in your current script which could be improved upon.

You should be using a more robust method to check for a form submission. Instead use:-

if($_SERVER['REQUEST_METHOD'] === 'POST'){

Another very important consideration is security. Any user input should be validated and sanitised.
If writing the input to a file like that, you can simply escape it with htmlspecialchars:-

fwrite($handle,"<b>" . htmlspecialchars($name) . "</b>:<br/>" . htmlspecialchars($content) . "<br/>");

Or do it at this stage, as these two lines as they are, are somewhat redundant:-

$name = htmlspecialchars($_POST['name']);
$content = htmlspecialchars($_POST['commentContent']);

Otherwise you are wide open to script injection.

Normally, using a database, I would escape on retrieval of the data, but since you are writing to an html file, it makes sense to do it there.

2 Likes