Skip to content
Advertisement

PHP Write HTML tags into file and display it

I want to make a simple comment system in PHP and my problem is when the user type ‘<‘ it disappear because it takes it to HTML code and mess my code. So what I need to do, when the user type this into the textarea: <stdio.h>, and post it, it should appear as <stdio.h>.

My PHP code:

I want to make a simple comment system in PHP and my problem is when the user type ‘<‘ it disappear because it takes it to HTML code and mess my code. So what I need to do, when the user type this into the textarea: <stdio.h>, and post it, it should appear as <stdio.h>.

My PHP code:

JavaScript

Advertisement

Answer

I am wondering why you are writing the file one byte at a time, there must be some really dodgy sample code out there somewhere. If you use htmlspecialchars() it will convert special characters to HTML entities

JavaScript

RESULT in File

JavaScript

PHP Manual htmlspecialchars()
And if you need it
PHP Manual htmlspecialchars_decode()

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement